Solved

simple servlet issue

Posted on 2013-01-25
12
390 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
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: 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

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Checkbox and ListView in Android Layout 4 65
eclipse argument 14 60
web application structure 18 76
Unhandled exception type Exception 18 28
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

813 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

12 Experts available now in Live!

Get 1:1 Help Now