Solved

simple servlet issue

Posted on 2013-01-25
12
391 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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
 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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.

821 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