helloServlet类:
接收form.jsp传来的method字符,执行相应的方法,请求转发到hello.jsp
package wsh.servlet; import javax.servlet.*; import javax.servlet.http.*; import java.io.IOException; public class HelloServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String method=request.getParameter("method"); if (method.equals("add")){ request.getSession().setAttribute("method","执行了find方法"); } if (method.equals("find")){ request.getSession().setAttribute("method","执行了find方法"); } request.getRequestDispatcher("jsp/hello.jsp").forward(request,response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }
form.jsp:
接收用户输入text内容提交helloServlet
<%-- Created by IntelliJ IDEA. User: wsh Date: 2022/5/28 Time: 22:20 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="/spring_mvc_servlet_war_exploded/hello" method="post"> <input type="text" name="method"> <input type="submit"> </form> </body> </html>
hello.jsp
显示数据session里面拿
<%-- Created by IntelliJ IDEA. User: wsh Date: 2022/5/28 Time: 22:21 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h2>Hello Spring-MVC</h2> ${sessionScope.method} </body> </html>
注意各文件路径是否一致!!!!
浙公网安备 33010602011771号