In a JAVA WEB application, how to get parameters in a servlet request and pass them to a jumped JSP page? For example, visit http://localhost:8088/bbs?id=1
When executing this bbs servlet, pass the value of the url parameter id to the bbs.jsp page?
1. First, you need to configure web.xml, see 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. Write 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.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 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, with the contents:
<%@ page contentType="text/html;charset=UTF-8"%> <%out.println(request.getAttribute("id")); %>Note that many people fail to pass parameters because they call doPost in the doGet method. Do not call doPost in the doGet method here.