Share on Google+Share on Google+

Pass parameters from JSP to Servlet

In this section, you will learn how to pass parameters from JSP page to servlet.

Pass parameters from JSP to Servlet

In this section, you will learn how to pass parameters from JSP page to servlet. For this purpose, we have used setAttribute() method. This method sets the value of the attribute for the request which is retrieved later in the servlet by passing the request object through the dispatcher method. The set value of the attribute is retrieved by the getAttribute() method of the request object.

Here is the code of jsptoServlet.jsp:

<[email protected] language="java"%>
request.setAttribute("Company Name","Roseindia");
String strViewPage="../GettingParameter";
RequestDispatcher dispatcher = request.getRequestDispatcher(strViewPage);
if (dispatcher != null){
dispatcher.forward(request, response);


Here is the code of

import javax.servlet.*;
import javax.servlet.http.*;

public class GettingParameter extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
	throws IOException {
ServletOutputStream out = res.getOutputStream();
String name = req.getParameter("name");
String address = req.getParameter("address");
out.println("Name is: " + req.getAttribute("Company Name"));
out.println("Address is: " + req.getAttribute("Address"));


Name is: Roseindia Address is: Rohini,Delhi



Posted on: November 18, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.


Advertisement null