73.【JavaWeb-01】(八)

简介: 73.【JavaWeb-01】
3.实现重定向(状态码:302)=《路径/ /》
1.重定向的定义:
一个web资源收到客户端的请求后,他会通知客户端去访问另一个web资源,这个过程就叫做重定向。
2.常见场景:  用户登入
void sendRedirect(String var1) throws IOException; 

package com.Jsxs.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 RedirectServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       resp.sendRedirect("/Servelete03_war/demo2");
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

向映射demo2进行跳转的操作

基本原理的操作:

resp.setHeader("Location","/Servelete03_war/demo2");
resp.setStatus(HttpServletResponse.SC_FOUND);  302
代替上面的操作: resp.sendRedirect("/Servelete03_war/demo2");

4.读取前端信息
请你说说重定向和转发的区别:
(1).相同点:
页面都会跳转
(2).不同点:
请求转发的时候: url不会改变
重定向的时候,url会改变
在jsp文件中,输入汉字的时候el表达式,需要加上:
<%@page contentType="text/html; charset=UTF-8" %>
为了防止产生乱码的现象。

转发:

Java

package com.Jsxs.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 Demo5 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      resp.setCharacterEncoding("UTF-8");
      resp.setContentType("text/html");
       resp.getWriter().println("哈哈哈,我是demo5页面");
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

JSP

<html>
<body>
<h2>Hello World!</h2>
<%@page contentType="text/html; charset=UTF-8" %>
<form action="${pageContext.request.contextPath}/demo5" method="get">
    用户名: <input type="text" name="username" placeholder="请输入您的用户名">
    密码: <input type="password" name="password" placeholder="请输入您的密码">
    <input type="submit">
</form>
</body>
</html>

利用了表单转发

重定向:
• 1
package com.Jsxs.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 Demo5 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      resp.setCharacterEncoding("UTF-8");
      resp.setContentType("text/html");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println(username+" "+password);
        resp.sendRedirect("/Servelete03_war/succes.jsp");
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
<html>
<body>
<h2>Hello World!</h2>
<%@page contentType="text/html; charset=UTF-8" %>
<form action="${pageContext.request.contextPath}/demo5" method="get">
    用户名: <input type="text" name="username" placeholder="请输入您的用户名">
    密码: <input type="password" name="password" placeholder="请输入您的密码">
    <input type="submit">
</form>
</body>
</html>

(十一)、HttpServletRequest(获取客户端信息)

HttpServletRequest代表客户端的请求,用户通过http协议访问服务器,Http请求
中的所有信息会被封装到HttpServletRequest,通过这个HttpServletRequest的方法
获得客户端的所有信息。

35.获取前端 传递的参数

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>登入</h1>
<div>
    <form action="${pageContext.request.contextPath}/demo1" method="post">
        用户名: <input type="text" name="username" placeholder="请输入您的账号"> <br/>
        密码: <input type="password" name="password" placeholder="请输入您的密码"> <br/>
        爱好:
        <input type="checkbox" name="s" value="girl"> 女孩
        <input type="checkbox" name="s" value="sing"> 唱歌
        <input type="checkbox" name="s" value="song"> 跳舞
        <input type="checkbox" name="s" value="playGrame"> 打牌
        <input type="submit"> <br/>
    </form>
</div>
</body>
</html>

succes.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>欢迎来到吉士先生的世界?</h1>
</body>
</html>

java

package com.Jsxs.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
public class Demo1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        响应编码
        resp.setCharacterEncoding("utf-8");
//        请求编码
        req.setCharacterEncoding("utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
//        获取多个
        String[] s = req.getParameterValues("s");
        System.out.println("=====================================");
        System.out.println(password);
        System.out.println(username);
        System.out.println(Arrays.toString(s));
        System.out.println("=====================================");
        resp.sendRedirect("/Servelete04_war/succes.jsp");
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

相关文章
|
Java
74.【JavaWeb -02】(四)
74.【JavaWeb -02】
47 0
|
Java Apache C++
74.【JavaWeb -02】(七)
74.【JavaWeb -02】
62 0
|
JavaScript 前端开发 Java
JavaWeb02
JavaWeb02
161 0
|
前端开发 网络协议 Java
javaweb的理解
javaweb的理解
72 0
|
存储
73.【JavaWeb-01】(六)
73.【JavaWeb-01】
37 0
|
Java
75.【JavaWeb-03】(二)
75.【JavaWeb-03】
50 0
|
网络协议 Java Maven
73.【JavaWeb-01】(二)
73.【JavaWeb-01】
56 0
|
Java Maven
73.【JavaWeb-01】(七)
73.【JavaWeb-01】
51 0
|
Java 应用服务中间件 Maven
73.【JavaWeb-01】(五)
73.【JavaWeb-01】
70 0

相关实验场景

更多
下一篇
DataWorks