Passing values from servlet to jsp

Step 1: ServletCall.jsp

 

[html]<%@ 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>Javadomain.in</title>
</head>
<body>
Click the below button to call the servlet, then servlet will print the value in the display.jsp file
<br>
<br>
<form action="ServletValues">
<input type="submit">
</form>
</body>
</html>[/html]

 

Step 2: ServletValues.java

 

[java]import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/ServletValues")
public class ServletValues extends HttpServlet {
private static final long serialVersionUID = 1L;
public ServletValues() {
super();

}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
String input = "Welcome to Javadomain.in";
request.setAttribute("servlet_value",input);
RequestDispatcher rd = request.getRequestDispatcher("display.jsp");
rd.forward(request, response);
}

}[/java]

 

Step 3: display.jsp

 

[html]<%@ 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>Javadomain.in</title>
</head>
<body>
The values from the servlet (ServletValues) is,
<br>
<br>
${servlet_value}
<!– servlet_value is set in the ServletValues servlet. –>
</body>
</html>[/html]

 

Note:

Do not forget to add the required jar files.

Download the servlet required jar files

Output: ServletCall.jsp

servlet call

display.jsp

display jsp

 

Thanks for reading this post…………..!!!

2 comments

  • sumit kher

    I am basically not a programmer and I am comparatively new to Java technology , so I was wondering what all topics should be covered up if i have to start java from the start and has any one
    studied or got any info regarding this 6 week java training online course http://www.wiziq.com/course/12145-the-6-week-complete-java-primer-with-training-certificate and should we also have knowledge of C language before we further move on to Advance Java topics??

  • Dear Friend Naveen,

    Don’t mistake me and don;t get angry me. am really hate when click anything in your website social networking sharing message will come . we will wait 60 seconds. nobody never like this. try to change your options. see this is my opinion and suggestion only. if you want to continue no problem for me.

Leave a Reply