Saturday 12, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Saturday, 15 March 2014

Servlet GET method to Pass URL param with example program

In this tutorial we are going to see about Servlet GET method and how to pass the param in URL with example program.
doGet()
1.protected void doGet(HttpServletRequest request,
2.   HttpServletResponse response) throws ServletException, IOException {
3.//code block
4. 
5.}

The doGet method called by the server (via the service method) to allow a servlet to handle a GET request.The GET method is default method to pass information from browser to server.The parameter passed using GET method is visible in browser URL, so GET method not recommanded for password and sensitive information. The GET method support 1024 characters can be sent in request.

Overriding this method to support a GET request also automatically supports an HTTP HEAD request. A HEAD request is a GET request that returns no body in the response, only the request header fields.

When overriding this method, read the request data, write the response headers, get the response's writer or output stream object, and finally, write the response data. It's best to include content type and encoding. When using a PrintWriter object to return the response, set the content type before accessing the PrintWriter object.

The servlet container must write the headers before committing the response, because in HTTP the headers must be sent before the response body.

Where possible, set the Content-Length header (with the ServletResponse.setContentLength(int) method), to allow the servlet container to use a persistent connection to return its response to the client, improving performance. The content length is automatically set if the entire response fits inside the response buffer.

When using HTTP 1.1 chunked encoding (which means that the response has a Transfer-Encoding header), do not set the Content-Length header.

The GET method should be safe, that is, without any side effects for which users are held responsible. For example, most form queries have no side effects. If a client request is intended to change stored data, the request should use some other HTTP method.

The GET method should also be idempotent, meaning that it can be safely repeated. Sometimes making a method safe also makes it idempotent. For example, repeating queries is both safe and idempotent, but buying a product online or modifying data is neither safe nor idempotent.

If the request is incorrectly formatted, doGet returns an HTTP "Bad Request" message.

Now see how to create servlet and pass parameter to servlet as query string.

1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project .

2. Create the Project called ServletExample as given below.

Servlet-dynamic web project

3. Create package called com.javatutorialscorner.servlet under ServletExample.

4. Create Servlet called GetUrlParamServlet as shown in figure.

Create Servlet

http servlet

5. Click Next it will show URL mapping.You can edit Servlet URL if you need.

6. Click Next it will show methods available  in HttpServlet. Select appropriate method you need.

http servlet Services

7. By default your servlet will be mapped in web.xml, if your servlet not mapped in your web.xml use the following configuration to map the servlet in your web.xml

Servlet Mapping

1.<servlet>
2.    <servlet-name>Your Servlet Name</servlet-name>
3.    <servlet-class>Fully Qulaified Servlet Class Name</servlet-class>
4.  </servlet>
5.  <servlet-mapping>
6.    <servlet-name>Your Servlet Name</servlet-name>
7.    <url-pattern>/URL to Call Servlet</url-pattern>
8.  </servlet-mapping>

web.xml

01.<?xml version="1.0" encoding="UTF-8"?>
03.  <display-name>ServetExample</display-name>
04.  <welcome-file-list>
05.    <welcome-file>index.html</welcome-file>
06.    <welcome-file>index.htm</welcome-file>
07.    <welcome-file>index.jsp</welcome-file>
08.    <welcome-file>default.html</welcome-file>
09.    <welcome-file>default.htm</welcome-file>
10.    <welcome-file>default.jsp</welcome-file>
11.  </welcome-file-list>
12.  <servlet>
13.    <servlet-name>GetUrlParamServlet</servlet-name>
14.    <servlet-class>com.javatutorialscorner.servlet.GetUrlParamServlet</servlet-class>
15.  </servlet>
16.  <servlet-mapping>
17.    <servlet-name>GetUrlParamServlet</servlet-name>
18.    <url-pattern>/GetUrlParamServlet</url-pattern>
19.  </servlet-mapping>
20. 
21.</web-app>

8. Add the required code inside doGet() method.

GetUrlParamServlet .java

01.package com.javatutorialscorner.servlet;
02. 
03.import java.io.IOException;
04.import java.io.PrintWriter;
05. 
06.import javax.servlet.ServletException;
07.import javax.servlet.http.HttpServlet;
08.import javax.servlet.http.HttpServletRequest;
09.import javax.servlet.http.HttpServletResponse;
10. 
11./**
12. * Servlet implementation class GetUrlParamServlet
13. */
14. 
15.public class GetUrlParamServlet extends HttpServlet {
16. private static final long serialVersionUID = 1L;
17.        
18.    /**
19.     * @see HttpServlet#HttpServlet()
20.     */
21.    public GetUrlParamServlet() {
22.        super();
23.        // TODO Auto-generated constructor stub
24.    }
25. 
26. /**
27.  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
28.  */
29. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
30. 
31.  response.setContentType("text/html");
32.  PrintWriter writer = response.getWriter();
33.  writer.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n"
34.    + "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\"><title>Java Tutorials Corner - Servlet GET method</title>"
35.    + "</head><body><h1>Java Tutorials Corner - Servlet GET method</h1><table><tr>"
36.    + "<td>Name : </td><td>"
37.    + request.getParameter("name")
38.    + "</td></tr><tr><td>Job : </td><td>"
39.    + request.getParameter("job") + "</td></tr></table></body></html>");
40. }
41. 
42. /**
43.  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
44.  */
45. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
46.  // TODO Auto-generated method stub
47. }
48. 
49.}

9. Now save and Run the servlet, Run As –> Run on Server – Select your web Server to run the servlet. ( see How to configure tomcat in eclipse

10.call the following URL see the output.

http://localhost:8080/ServletExample/GetUrlParamServlet?name=Appu.T&job=Software%20Engineer

Output

servlet output

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • Servlet - Hello World Sevlet using @WebServlet Annotation In this tutorial we are going to see about Hello World Servlet  program using Servlet 3 @WebServlet Annotation. public @interface WebServlet Annotation used to declare a servlet. This annotation is processed by the c… Read More
  • Servlet – Request Dispatcher forward In this tutorials we are going to see about Servlet Request Dispatcher forward with example program. void forward(ServletRequest request,ServletResponse response)throws ServletException,java.io.IOException Forwards a reques… Read More
  • Servlet – Read Select Box Value In this tutorials we are going to see how to read select box value using servlet program. Step by step instructions given below . 1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project . 2. C… Read More
  • Servlet – Form Submit using GET methodIn this tutorial we are going to see how to submit from in servlet using GET method.1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project . 2. Create the Project called ServletExample as given… Read More
  • Servlet - HTTP Status Codes In this tutorial we are going to see about HTTP status code with Servlet example program. See more about Http Status code click here - Http Status code in Detail (HTTP 1.1 Status code). List of status codes available in Http… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Servlet GET method to Pass URL param with example program Rating: 5 Reviewed By: eHowToNow