This example is a good example. This article mainly describes how to pass a value to the JSP page in servlet. In this example, the value is divided into three parts, 1. pass in a common string 2. pass in a common list 3. pass in a list with multiple maps. The page will process the passed values through jstl and El expressions.
1.Jspservletjstl. jsp----- Display the JSP page
<% @ Page contenttype = "text/html; charset = UTF-8 "iserrorpage =" true "%> <HTML>
2.Web. xml configuration------- The Web. xml file contains: <context-param> used to configure container (Tomcat) initialization parameters. <Servlet> Configure the association between the servlet name and the class and the content processed by the servlet. Listener: configure the container's response to the event, such as org. springframework. Web. util. introspectorcleanuplistener.
<! -- JSP and jstl test --> <servlet-Name> gojstl </servlet-Name> <servlet-class> second. gojstl </servlet-class> // class location </servlet> <servlet-mapping> <servlet-Name> gojstl </servlet-Name> <URL-pattern>/gojstl </url-pattern> // process access requests to the/gojstl path </servlet-mapping>
3.Servlet class Processing--------- Servlet mainly includes two methods: doget and dopost. The container selects which method to use based on different forms. The get and post requests will not be mentioned. For the difference between getparameter and getattribute, click the open link. For the difference between forward and sendredirect, click the open link.
Package second; import Java. io. ioexception; import Java. util. arraylist; import Java. util. hashmap; import Java. util. list; import Java. util. map; import javax. servlet. servletexception; import javax. servlet. HTTP. httpservlet; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. extends; public class gojstl extends httpservlet {public void doget (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {dopost (request, response);} public void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {request. setcharacterencoding ("UTF-8"); string username = request. getparameter ("username"); Request. setattribute ("username", username); // write the string parameter map map1 = new hashmap (); map1.put ("username", username + 1) to the session ); map1.put ("idcard", "111111111111111111"); map MAP2 = new hashmap (); map2.put ("username", username + 2); map2.put ("idcard ", "2222222222222222222"); List list1 = new arraylist (); list1.add (map1); list1.add (MAP2); List list2 = new arraylist (); list2.add ("map1 "); list2.add ("MAP2"); Request. setattribute ("pagelist1", list1); Request. setattribute ("pagelist2", list2); // Similarly, write the list parameter This. getservletcontext (). getrequestdispatcher ("/showresult. JSP "). forward (request, response); // page Jump // response. sendredirect ("finish. JSP ");}}
4.Display the processing page showresult. jsp---------- Jstl is a tag library of JSP, which facilitates the compilation of JSP files.
<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8 "%> <% @ taglib prefix =" C "uri =" http://java.sun.com/jsp/jstl/core "%> <HTML>