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.
3. Create package called com.javatutorialscorner.servlet under ServletExample.
4. Create Servlet called GetFormSubmitServlet as shown in figure.

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.

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
web.xml
8. Add the required code inside doGet() method.
GetFormSubmitServlet.java
9. Create html page in WebContent folder
formsubmit.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
After form submit.
Get method show param and values in browser url.

1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project .
2. Create the Project called ServletExample as given below.

3. Create package called com.javatutorialscorner.servlet under ServletExample.
4. Create Servlet called GetFormSubmitServlet as shown in figure.


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.

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"
?>
02.
<
web-app
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns
=
"http://java.sun.com/xml/ns/javaee"
xmlns:web
=
"http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation
=
"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id
=
"WebApp_ID"
version
=
"3.0"
>
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

After form submit.
Get method show param and values in browser url.

0 comments:
Post a Comment