企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
![](https://box.kancloud.cn/41617aef53a4ea80ea9731906cf85593_589x359.png) ![](https://box.kancloud.cn/39e65d19985c9a2339799f52a8244c4a_487x80.png) **案例-登录后跳转网站首页** LoginServlet.java ~~~ package zyw.servlet; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.*; import java.io.IOException; @WebServlet(name = "LoginServlet",urlPatterns = "/login") public class LoginServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String userName = "xiaoming"; String userPass = "123456"; String name = request.getParameter("name"); String passWord = request.getParameter("passWord"); if (!name.equals(userName)) { //账户不存在 request.setAttribute("errorMessage", "账户不存在"); request.getRequestDispatcher("/loginError.jsp").forward(request, response); } else if (!passWord.equals(userPass)) { //密码错误 request.setAttribute("errorMessage", "密码错误"); request.getRequestDispatcher("/loginError.jsp").forward(request, response); }else { //方法1 /* response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY); response.setHeader("Location","https://www.baidu.com/");*/ //方法2 response.sendRedirect("/hello/home.html"); } } } ~~~ home.html ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>home</title> </head> <body> <h2>我是网站的首页,欢迎访问</h2> </body> </html> ~~~ ![](https://box.kancloud.cn/63bcdce8ec20faece292e788e87b42b2_636x173.png)