一、转发和重定向的区别
转发
转发:转发就是服务器自己转发到服务器上的其他请求,是服务器行为
特点:
- 地址栏不会改变
- 可以携带参数
- 转发不可以访问自己web应用以外的资源
//转发:服务器把这个请求转向另外一个Servlet去处理; (地址栏不会变)
//RequestDispatcher ,需要使用RequestDispatcher来进行处理,我们需要获得这个类
//参数就是他要转发到的页面
req.getRequestDispatcher("/login.jsp").forward(req,resp);
重定向
重定向:服务器告诉客户端,你取请求另外一个地址,是客户端行为,但是是由服务器做的事情。
特点:
- 地址栏会变
- 不能懈怠参数
- 重定向可以访问web应用以外的资源
//重定向:服务器告诉客户端你应该请求另外一个地址; (地址栏会变)
resp.sendRedirect("https://www.baidu.com");
为了避免乱码问题,我们在可以设置编码格式的地方全部设置上编码格式,例如数据库,数据库字段,Servlet程序,请求,响应 , 前端,浏览器这样会尽可能的避免乱码问题。
二、Response中需要知道的重定向
将一个请求转换到另一个地址,通过响应对象重定向页面resp.sendRedirect(“info.jsp”);。
例一,我们访问时会跳转到info.jsp页面
编写Servlet类
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class ServletDemo4 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//重定向 到info界面
resp.sendRedirect("info.jsp");
//两秒钟自动跳转到百度,也可以跳转到自身页面,用于刷新,可以带参数,若不携带参数三则是刷新自身网页
//resp.setHeader("Refresh","2;url=http://www.baidu.com");
resp.setContentType("");//设置响应类型
resp.setCharacterEncoding("UTF-8");//设置响应编码格式为utf-8
}
}
}
}
编写info.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>你好</title>
</head>
<body>
<h1>第一次见你</h1>
</body>
</html>
配置web.xml
<servlet>
<servlet-name>Servlet3</servlet-name>
<servlet-class>org.westos.servlet.ServletDemo4</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet3</servlet-name>
<url-pattern>/q4</url-pattern>
</servlet-mapping>
三、Request中需要知道的请求转发
一、乱码解决方案
所有能设置编码的地方都统一设置:数据库,数据库字段,Servlet,请求,响应,前端,浏览器。
二、Request对象常用的方法
package org.westos.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class ServletDemo5 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println(req.getContextPath());//获得web项目路径
System.out.println(req.getRequestURI());//请求的URL路径
System.out.println(req.getRemoteUser());//获得远程的用户名
System.out.println(req.getRequestedSessionId());//获得SessionId
System.out.println(req.getServletPath());//获得请求servlet的url
System.out.println(req.getLocalAddr());//获得本地地址 防火墙
System.out.println(req.getLocalName());//获得本地名字
System.out.println(req.getLocalPort());//获得访问的端口号
}
}
三、Request对象接收前端用户提交的参数
1.编写前端登陆页面
- 表单的提交方式为get/post
- 表单提交的位置action:服务项目的路径/servlet对应的请求url
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登陆</title>
</head>
<body>
<%--
Form表单
action:代表要提交的路径,表单要提交到哪里 ,可以提交到一个Servlet
//提交到Servlet需要,获取到Web项目的路径下的Servlet
method: 提交表单的方式
--%>
<form action="${pageContext.request.contextPath}/r6"method="post">
<p>用户名:<input type="text" name="username"></p>
<p>密码:<input type="password" name="password"></p>
<p>
<input type="submit">
<input type="reset">
</p>
</form>
</body>
</html>
2.编写Servlet
package org.westos.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class ServletDemo6 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//处理乱码
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
//通过HttPServletRequest获得前端用户提交的信息
//
String username = req.getParameter("username");
String password = req.getParameter("password");
//连接数据库比较数据库中的信息是否匹配
// if (username.equals("admin")&&password.equals("123456")){
// System.out.println("登陆成功");//这里我们可以跳转至成功页面
// }else {
// System.out.println("登陆失败");//提示重新登陆
// }
System.out.println(username);
System.out.println(password);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
3.配置web.xml
<servlet>
<servlet-name>Servlet6</servlet-name>
<servlet-class>org.westos.servlet.ServletDemo6</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet6</servlet-name>
<url-pattern>/r6</url-pattern>
</servlet-mapping>
4.测试,登陆localhost:8080/web1/login.jsp
用户台输出为:
在这里我们可以中文和英语都试一下,查看是否有乱码出现,如果有,那么就在能设置编码格式的地方都设置上编码格式,这样几乎可以避免乱码文图产生。
刘瑞龙
123456
4、Request实现请求转发
我是这样设计的,刚开始有个登录页面,首先,如果两次密码相等,那么久重定向到百度页面,否则就转发到本页面重新登录
1.编写前端登录页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>注册</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/r1"method="post">
<p>用户名:<input type="text" name="username"></p>
<p>密码:<input type="password" name="password"></p>
<p>确认密码:<input type="password" name="password1"></p>
<p>
<input type="submit">
<input type="reset">
</p>
</form>
</body>
</html>
2.编写Servlet
package org.westos.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class loginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//处理乱码
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
//获得前端的请求
String username = req.getParameter("username");
String password = req.getParameter("password");
String password1 = req.getParameter("password1");
//判断前端输入的密码是否正确
if (password.equals(password1)){
//如果成功,就让他重定向到百度页面
resp.sendRedirect("https://www.baidu.com");
}else {
//如果不成功,就继续转发到注册页面
req.getRequestDispatcher("/login.jsp").forward(req,resp);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
3.配置web.xml
<servlet>
<servlet-name>Servlet</servlet-name>
<servlet-class>org.westos.servlet.loginServlet</servlet-class>
</servlet>
<!--===============================-->
<servlet-mapping>
<servlet-name>Servlet</servlet-name>
<url-pattern>/r1</url-pattern>
</servlet-mapping>
5.验证结果
密码相同的情况下,成功重定向到百度页面,另外我们可以看到地址栏也发生了变化。
密码不相同的情况下,还是转发到本页面,继续登录,另外地址栏不会发生变化