zoukankan      html  css  js  c++  java
  • 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);
        }
    
    }
  • 相关阅读:
    微信小程序错误码参考大全
    关于vagrant一个虚拟机搭建多个项目配置(总结)
    Vagrant安装完lnmp后,配置linux和windows共享文件并配置虚拟主机访问项目
    vagrant的centos镜像,怎么用root用户登录?
    Vagrant系列(二)----Vagrant的配置文件Vagrantfile详解
    过滤数组中的空数组array_filter()
    getMessage(),getFile,getLine获取异常用法
    测试工程师的福利!各远程移动测试平台对比分析
    学习这篇总结后,你也能做出天天快报一样的推荐系统
    别被官方文档迷惑了!这篇文章帮你详解yarn公平调度
  • 原文地址:https://www.cnblogs.com/fengfuwanliu/p/11441236.html
Copyright © 2011-2022 走看看