In JSP, we print the different attributes of the Student object like the following: Student information: In order to pass a business object or POJO from servlet to JSP, you can pass it as an attribute using the setAttribute() method described above.įollowing is an example for passing a Student object from servlet to JSP:Īt the server-side, the Student object is set as a request attribute: Student student = new Student() This technique is mostly used when you want to pass a reasonable number of simple attributes. In JSP, you can access the parameter like:.ndRedirect( "home.jsp?name=Hussein Terek" ) At the server-side, redirect the response to the JSP page and append the parameters directly in the URL as the following:.The second way of passing data from servlet to JSP is through redirecting the response to the appropriate JSP and appending the attributes in the URL as a query string. My name is getServletContext().getAttribute("name") GetServletContext().setAttribute("name","Hussein Terek") At the server-side, add the attribute to the context then forward the request to the JSP page like the following:.Context attributes are meant for infrastructure, such as shared connection pools. By definition, a context attribute exists locally in the VM where it is defined, so it isn’t available on distributed applications. This technique binds the attribute to the whole context of the application, the attribute is available to all servlets (thus JSP) in that context.getServletContext().setAttribute(Name,Value). My name is request.getSession().getAttribute("name") Request.getSession().setAttribute("name","Hussein Terek")
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |