java 请求转发_JavaWeb_请求转发

请求的转发和重定向:

1)本质区别:请求的转发只发出一次请求,而重定向则发出来两次请求。

具体:

①请求的转发:地址栏是初次发出请求的地址

请求的重定向:地址栏不再是初次发出的请求地址,地址栏为最后响应的那个地址

②请求转发:在最终的Servlet中,request对象和中转的那个request是同一个对象

请求的重定向:在最终的Servlet中,request对象和中转的那个request不是同一个对象

(3)请求的转发:只能转发给当前WEB应用的资源

请求的重定向:可以重定向到任何资源。

④请求转发:/ 代表的是当前WEB应用的根目录

请求重定向:/ 代表的是当前WEB站点的根目录

JSP

Title

ForwardServlet

RedirectServlet

Servlet

public class ForwardServlet extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

System.out.println("ForwardServlet's doGet");

/**

* 请求的转发

* 1.调用HttpServletRequest的getRequestDispatcher()方法获取RequestDispatcher对象

* 调用getRequestDispatcher()需要传入要转发的地址

*/

String path = "/TestServlet";

RequestDispatcher requestDispatcher = request.getRequestDispatcher(path);

/**

* 2.调用HttpServletRequest的forward(request,response)进行请求的转发

*/

requestDispatcher.forward(request,response);

}

public class TestServlet extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

System.out.println("TestServlet's doGet");

}

}

@WebServlet(name = "RedirectServlet")

public class RedirectServlet extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

System.out.println("RedirectServlet's doGet");

//执行请求的重定向,直接调用response,sendRedirect(path)方法

//path为要重定向的地址

String path = "TestServlet";

response.sendRedirect(path);

}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的JavaWeb登录功能实现代码示例: ```java // 导入必要的包 import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException { // 设置响应内容类型 response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html><head><title>Login</title></head><body>"); out.println("<h1>JavaWeb登录功能实现</h1>"); // 获取表单参数 String username = request.getParameter("username"); String password = request.getParameter("password"); // 检查登录信息是否正确 if (username.equals("admin") && password.equals("admin")) { // 创建会话并将用户名存储在会话中 HttpSession session = request.getSession(true); session.setAttribute("username", username); // 重定向到成功页面 response.sendRedirect("success.jsp"); } else { // 登录失败 out.println("<p>用户名或密码错误,请重新输入!</p>"); out.println("<a href='login.jsp'>返回登录页面</a>"); } out.println("</body></html>"); out.close(); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException { doGet(request, response); } } ``` 在上述代码中,我们使用了 `doGet` 方法来处理 GET 请求和 `doPost` 方法来处理 POST 请求。当用户提交登录表单时,我们获取表单中的用户名和密码,并在后台验证。如果登录信息正确,我们创建一个会话并将用户名存储在会话中,然后重定向到成功页面。如果登录信息不正确,则返回登录页面并显示错误消息。 这只是一个简单的示例,实际的登录功能可能需要更多的验证和安全措施。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值