For details about how to obtain the parameters in the servlet request on the jsp page, jspservlet
In a java web application, how does one obtain parameters in the servlet request and pass them to the redirected JSP page? For example, access http: // localhost: 8088/bbs? Id = 1
When executing this bbs servlet, pass the url parameter id value to the bbs. jsp page?
1. Configure web. xml as shown in the following Configuration:
<servlet> <servlet-name>bbs</servlet-name> <servlet-class> org.openjweb.core.servlet.BBSServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>bbs</servlet-name> <url-pattern>/bbs</url-pattern> </servlet-mapping>
2. Compile the servlet class:
Package org. openjweb. core. servlet; import java. io. IOException; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; public class BBSServlet extends HttpServlet {private static final long serialVersionUID = 1L; public BBSServlet () {super (); // TODO Auto-generated constructor stub} protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// http://bbs.csdn.net/topics/90438353 request. setCharacterEncoding ("UTF-8"); // set the encoding String id = request. getParameter ("id"); request. setAttribute ("id", id); request. getRequestDispatcher ("/bbs. jsp "). forward (request, response);} protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet (request, response );}}
Create a bbs. jsp file in the application root directory. The content is:
<%@ page contentType="text/html;charset=UTF-8"%> <%out.println(request.getAttribute("id")); %>
Note that many people fail to pass the parameter because the doPost method is called. Here, the doGet method does not call doPost.