Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

simple servlet issue

Posted on 2013-01-25
12
Medium Priority
?
398 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 28

Accepted Solution

by:
rrz earned 1400 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 28

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 200 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
 
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 400 total points
ID: 38826598
ops, sorry... the second line must be:
response.setHeader("Content-Type", "text/html;charset=UTF-8");
0
 
LVL 28

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 28

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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.
Suggested Courses

885 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