Thursday 10, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Sunday, 16 March 2014

Servlet – Form Submit using GET method

In 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 below.
Servlet-dynamic web project
3. Create package called com.javatutorialscorner.servlet under ServletExample.
4. Create Servlet called GetFormSubmitServlet 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>GetFormSubmitServlet</servlet-name>
14.    <servlet-class>com.javatutorialscorner.servlet.GetFormSubmitServlet</servlet-class>
15.  </servlet>
16.  <servlet-mapping>
17.    <servlet-name>GetFormSubmitServlet</servlet-name>
18.    <url-pattern>/GetFormSubmitServlet</url-pattern>
19.  </servlet-mapping>
20. 
21.</web-app>

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

GetFormSubmitServlet.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 GetFormParamServlet
13. */
14. 
15.public class GetFormSubmitServlet extends HttpServlet {
16. private static final long serialVersionUID = 1L;
17.        
18.    /**
19.     * @see HttpServlet#HttpServlet()
20.     */
21.    public GetFormSubmitServlet() {
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.  // TODO Auto-generated method stub
31.   
32.  response.setContentType("text/html");
33.  PrintWriter writer = response.getWriter();
34.  writer.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n"
35.    + "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\"><title>Java Tutorials Corner - Servlet GET method</title>"
36.    + "</head><body><h1>Java Tutorials Corner - Servlet GET method</h1><table><tr>"
37.    + "<td>Name : </td><td>"
38.    + request.getParameter("name")
39.    + "</td></tr><tr><td>Job : </td><td>"
40.    + request.getParameter("job") + "</td></tr></table></body></html>");
41. }
42. 
43. /**
44.  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
45.  */
46. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
47.  // TODO Auto-generated method stub
48. }
49. 
50.}

9. Create html page in WebContent folder

formsubmit.html

01.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
02.<html>
03.<head>
04.<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
05.<title>Java Tutorials Corner - Servlet Form Submit using GET method</title>
06.</head>
07.<body>
08.<h1>Java Tutorials Corner - Servlet Form Submit using GET method</h1>
09.<form action="GetFormSubmitServlet" method="get">
10. 
11.<table>
12.<tr>
13.<td>Name : </td>
14.<td><input type="text" name="name"/> </td>
15.</tr>
16.<tr>
17.<td>Job : </td>
18.<td><input type="text" name="job"/></td>
19.</tr>
20.</table>
21.<input type="submit" value="Submit"/>
22.</form>
23.</body>
24.</html>

In above html page in form tag method="get"to mention the form submit using get method.The default method to submit form is GET.

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

11.call the URL which is mapped in web.xml.

http://localhost:8080/ServletExample/formsubmit.html

Output

servlet submit from

After form submit.

Get method show param and values in browser url.

servlet form submit output

Shop and help us

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

Related Posts:

  • Servlet – ServletContext Interface In this tutorial we are going to see about ServletContext Interface with example program. public interface ServletContext Defines a set of methods that a servlet uses to communicate with its servlet container, for example,… Read More
  • Servlet – HTTP Servlet Response In this tutorials we are going to see about Http Servlet response.This tutorial written based on Servlet 3.0. HttpServletResponse Extends the ServletResponse interface to provide HTTP-specific functionality in sending a res… Read More
  • Servlet – Read Context Param using ServletContext Interface In this tutorial we are going to see about, how to read Context param using ServletContext interface. In previous tutorials I explained about ServletContext interface with example program (read single context-param).Click he… Read More
  • Servlet - HTTP Servlet Request In this tutorials we are going to see about HttpServletRequest . HttpServletRequest Extends the ServletRequest interface to provide request information for HTTP servlets. This tutorial written based on Servlet 3.0. The serv… Read More
  • Servlet – ServletConfig Interface In this tutorial we are going to see about ServletConfig interface with example program. A servlet configuration object used by a servlet container to pass information to a servlet during initialization. Methods in ServletC… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Servlet – Form Submit using GET method Rating: 5 Reviewed By: eHowToNow