For details about how to obtain the parameters in the servlet request on the jsp page, jspservlet

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.