这期内容当中小编将会给大家带来有关怎么在jsp页面中获取servlet请求的参数,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
1.首先要配置web.xml,见下面的配置:
<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.编写servlet类:
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"); //设置编码 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); } }
在应用根目录创建bbs.jsp文件,内容为:
<%@ page contentType="text/html;charset=UTF-8"%> <% out.println(request.getAttribute("id")); %>
注意很多人传递参数不成功是因为是在doGet方法中调用doPost,这里doGet方法不要调用doPost.
上述就是小编为大家分享的怎么在jsp页面中获取servlet请求的参数了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。