场景描述:根据URL传递来的参数,请求的jsp页面获取参数值,http://localhost:8083/myJsp.jsp?mid=1020
http://localhost:8083/myJsp?mid=1020(请求servlet)
jsp脚本标记<%%>
jsp输出标记<%=输出内容%> <% out.println("输出内容");%>
MyJsp.jsp jsp页面获取参数值并输出:
<% String mid = request.getParameter("mid"); //可以获取getAttribute值。 out.println("mid:" + mid); %> <% //String mid2 = (String)request.getAttribute("mid"); //out.println("mid2:" + mid2); %>
MyJsp.java servlet获取参数并转发
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String mid = request.getParameter("mid"); System.out.println("mid:" + mid); request.setAttribute("mid", mid); //转发 RequestDispatcher rd = this.getServletContext().getRequestDispatcher("/MyJsp.jsp"); rd.forward(request, response); //重定向 // response.sendRedirect("/MyJsp.jsp"); }
web.xml
<servlet> <servlet-name>MyJsp</servlet-name> <servlet-class>com.sms.test.MyJsp</servlet-class> </servlet> <servlet-mapping> <servlet-name>MyJsp</servlet-name> <url-pattern>/MyJsp</url-pattern> </servlet-mapping>