JavaWeb-HttpServletRequest举例解析

HttpServletRequest

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

JavaWeb-HttpServletRequest举例解析_第1张图片

小例子:

RequestServlet

package com.edwin.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
 * @author Edwin D
 * @date 2020.6.4 上午 9:16
 */
public class RequestServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        请求处理
        System.out.println("请求成功!");
        String Un = req.getParameter("Username");
        String Pwd = req.getParameter("Password");
        System.out.println(Un + ":" + Pwd);

//        重定向的时候一定要注意路径问题,否则极其容易出现404。
//        404代表路径问题,代码正常。而500则代表代码问题。
        resp.sendRedirect("/response_war/JumpSuccess.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

初始化的Jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<h2>Hello World!h2>
<%--这里提价的路径,需要寻找到项目的路径--%>
<%--这里的${pageContext.request.contextPath}代表当前的项目    --%>
<form action="${pageContext.request.contextPath}/Request" method="get">
    用户名:<input type="text" name="Username">   <br>
    密码:<input type="password" name="Password"> <br>
    <input type="submit">
form>
body>
html>

跳转的Jsp

<%--
  Created by IntelliJ IDEA.
  User: 元
  Date: 2020.6.4
  Time: 上午 9:33
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Successtitle>
head>
<body>
<h1>Success Jump!h1>
body>
html>

xml:

<servlet>
    <servlet-name>Requestservlet-name>
    <servlet-class>com.edwin.servlet.RequestServletservlet-class>
servlet>
<servlet-mapping>
    <servlet-name>Requestservlet-name>
    <url-pattern>/Requesturl-pattern>
servlet-mapping>

输出效果:

JavaWeb-HttpServletRequest举例解析_第2张图片

跳转:

JavaWeb-HttpServletRequest举例解析_第3张图片

获取参数,请求转发

JavaWeb-HttpServletRequest举例解析_第4张图片

getParameter()//适用于String
getParameterValues()//适用于数组

代码举例:
JavaWeb-HttpServletRequest举例解析_第5张图片
LoginServlet:

package com.edwin.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;
/**
 * @author Edwin D
 * @date 2020.6.4 下午 12:50
 */
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 Un = req.getParameter("Username");
        String Pwd = req.getParameter("Password");
        String[] hobbys = req.getParameterValues("hobbys");
        System.out.println("++++++++++++++++++++++++++++++++++");
        System.out.println(Un);
        System.out.println(Pwd);
        System.out.println(Arrays.toString(hobbys));
        System.out.println("++++++++++++++++++++++++++++++++++");

        System.out.println(req.getContextPath());
//        通过请求转发
//        这里的“/”代表了当前Web应用。
        req.getRequestDispatcher("/Success.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

Jsp1:

<%--
  Created by IntelliJ IDEA.
  User: 元
  Date: 2020.6.4
  Time: 上午 12:48
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录title>
head>
<body>
    <h1>登录h1>
        <form action="${pageContext.request.contextPath}/Login" method="post">
            用户名:<input type="text" name="Username">  <br>
            密码:<input type="password" name="Password"> <br>
            爱好:
            <input type="checkbox" name="hobbys" value="跑步">跑步
            <input type="checkbox" name="hobbys" value="游泳">游泳
            <input type="checkbox" name="hobbys" value="篮球">篮球
            <input type="checkbox" name="hobbys" value="约会">约会
            <br>
            <input type="submit">
        form>
body>
html>

Jsp2:

<%--
  Created by IntelliJ IDEA.
  User: 元
  Date: 2020.6.4
  Time: 下午 3:26
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Successtitle>
head>
<body>
<h1>登录成功h1>
body>
html>

Xml:

<servlet>
    <servlet-name>Loginservlet-name>
    <servlet-class>com.edwin.servlet.LoginServletservlet-class>
servlet>
<servlet-mapping>
    <servlet-name>Loginservlet-name>
    <url-pattern>/Loginurl-pattern>
servlet-mapping>

效果:

JavaWeb-HttpServletRequest举例解析_第6张图片

点击“登录”:

JavaWeb-HttpServletRequest举例解析_第7张图片

后台输出:

参考文献

《【狂神说Java】JavaWeb入门到实战》

视频连接

2020.06.04

你可能感兴趣的:(JavaWeb,Servlet)