SEND DATA FROM JSP TO SERVLET
How to send data
from Servlet to Jsp page
Now we will learn
how to send user data to servlet.This is easy but it is useful for lots of situations.
In Jsp page we can get user data in many ways and we can pass data to servlet again
in many ways. One of them is to send with form. To use form we should have some
knowledge about html forms.
Html Forms :
<form> is
the root tag. This tag has some attributes for example some of them:
Action : you can
put your servlet name to here if you want to pass data to servlet or you can
put here struts action that will be in another page.
Method : there is
2 methods to send data to servlet. One of them is the get and the other is the
post. If you select get method you can see your data from url. If you send as
post you can not see your data in url. This is more secured. When you are
programming to send data to another phase get methods gives you some flexibility
about adding extra data. Of course you can add extra data with post method but
you should use a little bit more advanced javascript.
<input> :
this tag can be used as container for the user data. You can get user data with
this tag. This tag has one attribute that is very useful at the servlet part.
Name : whatever
you write to here you can get this value with the same name at the servlet
side.
Type: To achieve
the sending data you need to use button you can define them with this
attribute.
<%@ page language="java" contentType="text/html;
charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html
PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1">
<title>Form</title>
</head>
<body>
<form action="servlet"
method="get">
<input name="field1"></input>
<input name="field2"></input>
<input name="field3"></input>
<input type="submit"/>
</form>
</body>
</html>
Servlet code is :
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class servlet
*/
public class servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public servlet() {
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
PrintWriter out = response.getWriter();
String str = request.getParameter("field1");
String str2 = request.getParameter("field2");
String str3 = request.getParameter("field3");
out.println("You typed : "+str+"\n"+str2+"\n"+str3);
}
/**
* @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("DoPost");
}
}
Picture 1
Picture 2
You can see the url contains data that the user can see.


Hi Dear Sumit Kher,
ReplyDeletein my opinion starting to java does not require that having knowledge of c or c++ but better if you would have. However i think it is necessary to have OOP ( Object Oriented Programming) knowledge to code nice Java if you do not have OOP you can also code but not as better as you had(Engineering comes from OOP). According to your desire you can continue Java as SE(Standart Edition), EE(Enterprise Edition) or advanced one that is ME(Mobile Ed.) for example symbian or android or tizen... As i read the plan of your site, you can learn the basics of Java it can be used in all areas (SE-EE-ME) then you can continue to improve your OOP skill that really makes you different (innovational).