• JavaWeb-----ServletRequest、ServletResponse和HttpServletRequest、HttpServletResponse


    JSP页面

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
        <form action="${pageContext.request.contextPath}/login" method="post">
            <span>用户名:</span><input type="text" name="userName"> <span>密码:</span><input
                type="text" name="userPwd"> <input type="submit" value="提交">
        </form>
    </body>
    </html>

    Web.xml配置

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
        id="WebApp_ID" version="3.1">
        <display-name>JspAndServlet</display-name>
        <welcome-file-list>
            <welcome-file>login.jsp</welcome-file>
        </welcome-file-list>
    
        <servlet>
            <servlet-name>loginServlet</servlet-name>
            <servlet-class>com.servlet.LoginServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>loginServlet</servlet-name>
            <url-pattern>/login</url-pattern>
        </servlet-mapping>
    </web-app>

    1. ServletRequest、ServletResponse

         Servlet类实现Servlet接口

    •    ServletRequest: 封装了请求信息. 可以从中获取到任何的请求信息
    •    ServletResponse: 封装了响应信息, 如果想给用户什么响应, 具体可以使用该接口的方法实现
    package com.servlet;
    
    import java.io.IOException;
    
    import javax.servlet.Servlet;
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * 实现Servlet接口
     * 
     * @author yyx 2019年9月1日
     */
    public class LoginServlet implements Servlet {
    
        @Override
        public void service(ServletRequest servletRequest, ServletResponse servletResponse)
                throws ServletException, IOException {
            // servletRequest需要强制转换成HttpServletRequest才能获取Http相关信息
            HttpServletRequest req = (HttpServletRequest) servletRequest;
    
            String userName = req.getParameter("userName");
            String userPwd = req.getParameter("userPwd");
            System.out.println(userName + " " + userPwd);
    
            String requestURI = req.getRequestURI();
            System.out.println(requestURI);
    
            String method = req.getMethod();
            System.out.println(method);
    
            String queryString = req.getQueryString();
            System.out.println(queryString);
    
            String servletPath = req.getServletPath();
            System.out.println(servletPath);
    
            // 设置响应的内容类型
            HttpServletResponse resp = (HttpServletResponse) servletResponse;
            resp.setCharacterEncoding("UTF-8");
    
        }
    
        @Override
        public void destroy() {
    
        }
    
        @Override
        public ServletConfig getServletConfig() {
            return null;
        }
    
        @Override
        public String getServletInfo() {
            return null;
        }
    
        @Override
        public void init(ServletConfig arg0) throws ServletException {
    
        }
    
    }

    2. HttpServletRequest、HttpServletResponse

         Servlet类实现继承HttpServlet

    •     HttpServletRequest继承自ServletRequest 
    •     HttpServletResponse继承自ServletResponse
    package com.servlet;
    
    import java.io.IOException;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    /**
     * 继承HttpServlet类
     * @author yyx 2019年9月1日
     */
    public class LoginServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String userName=req.getParameter("userName");
            String userPwd=req.getParameter("userPwd");
            System.out.println(userName+" "+userPwd);
            
            String requestURI = req.getRequestURI();
            System.out.println(requestURI);
            
            String method = req.getMethod();
            System.out.println(method); 
            
            String queryString = req.getQueryString();
            System.out.println(queryString); 
            
            String servletPath = req.getServletPath();
            System.out.println(servletPath); 
            
            //设置响应的内容类型
            resp.setCharacterEncoding("UTF-8");
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    
    }
  • 相关阅读:
    【BZOJ3533】向量集(SDOI2014)-线段树+凸壳+二分
    【BZOJ4869】相逢是问候(六省联考2017)-扩展欧拉定理+线段树
    【BZOJ4012】开店(HNOI2015)-动态点分治+set
    【BZOJ1095】捉迷藏(ZJOI2007)-动态点分治+堆
    【BZOJ2299】向量(HAOI2011)-裴蜀定理
    【BZOJ4942】整数(NOI2017)-线段树+压位
    【BZOJ3594】方伯伯的玉米田(SCOI2014)-DP+二维树状数组
    背包DP专题
    【2018.11.7】【luoguNOIp 热身赛】解题报告及总结
    【一天一DP计划】状压DP
  • 原文地址:https://www.cnblogs.com/fengfuwanliu/p/11441236.html
Copyright © 2020-2023  润新知