Solved

simple servlet issue

Posted on 2013-01-25
12
384 Views
Last Modified: 2013-02-14
I wrote below simple servlet


package com.gp.java;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class SimpleServlet
 */
@WebServlet(description = "SimpleServlet desc", urlPatterns = { "/SimpleServletUP" })
public class SimpleServlet extends HttpServlet {
      private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SimpleServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

      /**
       * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            response.setContentType("type/html");
            System.out.println("hello from get");
            PrintWriter writer=response.getWriter();
            writer.println("<h3>hello in html</h3>");
            
      //      String userNam=request.getParameter("userName");
      //      writer.println("heello"+userNam);
            
      }

      /**
       * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
      }

}


When I run instead of printing
'hello in html' in the page and
'hello from get' on console
It is trying to open the servet source as in the attachment. How can i fix this issue.

 please advise.
Any links resources, sample code, ideas highly appreciated. Thanks in advance
SimpleSErvlet.jpg
0
Comment
Question by:gudii9
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 27

Accepted Solution

by:
rrz earned 350 total points
ID: 38821086
>response.setContentType("type/html");  
Remove that line or try using
response.setContentType("text/html");
0
 
LVL 7

Author Comment

by:gudii9
ID: 38822238
>response.setContentType("type/html");  

response.setContentType("text/html");

both above lines you mentioned are same right. Please advise
0
 
LVL 27

Expert Comment

by:rrz
ID: 38822274
No, they are not the same. You must specify the MIME type of the content.  
Look at list towards the bottom of the following page.
http://en.wikipedia.org/wiki/Internet_media_type
0
 
LVL 7

Author Comment

by:gudii9
ID: 38824243
>>No, they are not the same. You must specify the MIME type of the content.  

How they are different.

For me they look same as below

>>response.setContentType("type/html");  
>>Remove that line or try using
>>response.setContentType("text/html");


How should i modify the servlet code. can you please post whole servlet code

package com.gp.java;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class SimpleServlet
 */
@WebServlet(description = "SimpleServlet desc", urlPatterns = { "/SimpleServletUP" })
public class SimpleServlet extends HttpServlet {
      private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SimpleServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

      /**
       * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            response.setContentType("type/html");
            System.out.println("hello from get");
            PrintWriter writer=response.getWriter();
            writer.println("<h3>hello in html</h3>");
           
      //      String userNam=request.getParameter("userName");
      //      writer.println("heello"+userNam);
           
      }

      /**
       * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
      }

}
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 50 total points
ID: 38824250
Does any servlet work on the same container?
0
 
LVL 7

Author Comment

by:gudii9
ID: 38824265
yes. other servlets are working fine. For example below servlet worked perfect

package com.gp.java;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class XmlServlet
 */

public class XmlServlet extends HttpServlet {
      private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public XmlServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

      /**
       * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            System.out.println("hello from get of xml serv");
            PrintWriter writer=response.getWriter();
            writer.println("<h3>hello in html of xml serv</h3>");
      
                  String userNam=request.getParameter("userName");
                  writer.println("heello"+userNam);
                  String[] location=request.getParameterValues("location");
                  writer.println("u at"+location.length+"places");
      }
      

      /**
       * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            System.out.println("hello from post of xml serv");
            PrintWriter writer=response.getWriter();
            writer.println("<h3>hello in html of xml serv</h3>");
      
                  String userNam=request.getParameter("userName");
                  writer.println("heello"+userNam);
                  String[] location=request.getParameterValues("location");
                  writer.println("u at"+location.length+"places");
      }

}
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 7

Author Comment

by:gudii9
ID: 38824382
even below worked perfect

package com.gp.java;
 
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
@WebServlet(description = "ParameterName desc", urlPatterns = { "/ParameterNameUP" })
public class ParameterName extends javax.servlet.http.HttpServlet implements
        javax.servlet.Servlet {
 
    public ParameterName() {
        super();
    }
 
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {/*
         
        PrintWriter pw = response.getWriter();
 
                //
                // Let's obtains parameters name here!
                //
        Enumeration enumeration = request.getParameterNames();
        while (enumeration.hasMoreElements()) {
            String parameterName = (String) enumeration.nextElement();
            pw.println("Hello Parameter = " + parameterName);
        }
        pw.close();
    */
          
    response.setContentType("text/html");
    PrintWriter writer=response.getWriter();
    String userName=request.getParameter("name");
    HttpSession session=request.getSession();
    if( userName!="" & userName != null){
          session.setAttribute("savedUserName", userName);
    }
   
    writer.println("Hellllooo request"+userName);
    writer.println("Hellllooo session"+(String)session.getAttribute("savedUserName"));
   
   
   
    }
 
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
    }
}
0
 
LVL 16

Expert Comment

by:Valeri
ID: 38826454
As I can see the servlets that work as you expecting don't have this line!
Btw try to remove "response.setContentType("type/html");" and to replace it with:

response.setHeader("Cache-Control", "no-cache");
response.setHeader("Content-Type", "text/xml;charset=UTF-8");
0
 
LVL 16

Assisted Solution

by:Valeri
Valeri earned 100 total points
ID: 38826598
ops, sorry... the second line must be:
response.setHeader("Content-Type", "text/html;charset=UTF-8");
0
 
LVL 27

Expert Comment

by:rrz
ID: 38838191
>When I run instead of printing
>'hello in html' in the page and
>'hello from get' on console
>It is trying to open the servet source as in the attachment. How can i fix this issue.

The problem is the following line
>response.setContentType("type/html");  
The browser doesn't know what to do with the response of  that content type. It asks the Operating System of the client to take control.

The working code you posted at
http://www.experts-exchange.com/Programming/Languages/Java/Q_28009529.html#a38824265 
doesn't send a content type. Therefore the content type is assumed to be text/html
The last code you posted has the line  
response.setContentType("text/html");  
Thus, the browser understands what to do with that response.
0
 
LVL 7

Author Comment

by:gudii9
ID: 38873334
>>response.setContentType("text/html");  

I am confused bit. should  i keep above line or not. I believe you are suggesting to remove.What is the use of that line.
when i have to put it, when i should not put it. please advise
0
 
LVL 27

Expert Comment

by:rrz
ID: 38889550
>>response.setContentType("text/html");    
The default type is   text/html  .   Therefore the above line is not necessary.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
split string containing \r\n in Java 46 38
How can I   ajax html table  rows? 20 63
Apps blocked by Java 9 63
map related example 6 36
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now