75.【JavaWeb-03】(三)

简介: 75.【JavaWeb-03】

(十九)、小结对18之前的

package com.Jsxs.Http;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLDecoder;
import java.util.Date;
import java.util.Properties;
public class Demo1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//  1.如果想要输出的数据是gbk,那么只需要resp响应头即可,可以不用设置rep。
        resp.setCharacterEncoding("gbk");
        req.setCharacterEncoding("UTF-8");
        resp.getWriter().write(resp.getCharacterEncoding());
        resp.getWriter().write("你好,我是getWriter()!");
//  2.进行设置响应界面的设置,什么界面以及编码格式
        resp.setContentType("text/html;charset=UTF-8");
        resp.getWriter().write(resp.getContentType());
//  3.getOutputStream和getWriter这两个方法互相排斥,调用了其中的任何一个方法后,就不能再调用另一方法。
//        ServletOutputStream outputStream = resp.getOutputStream();
//        outputStream.println(12123);
//  4.是为了解决相对路径的问题,可返回站点的根路径。
         resp.getWriter().write(req.getContextPath());
        resp.getWriter().write("你好");
//===========================================================================
//        ServletContext
        ServletContext servletContext = this.getServletContext();
//   1.存servlet和取servlet.
        servletContext.setAttribute("name","吉士先生");
        String name = (String) servletContext.getAttribute("name");
        resp.getWriter().write(name);
//  2.是为了解决相对路径的问题,可返回站点的根路径。
        String contextPath = servletContext.getContextPath();
        resp.getWriter().write(contextPath);
//  3.请求转发....
//        RequestDispatcher requestDispatcher = servletContext.getRequestDispatcher("/Demo2");
//        requestDispatcher.forward(req,resp);
//  4.获取资源
        InputStream is = servletContext.getResourceAsStream("/WEB-INF/classes/db.properties");
        Properties properties = new Properties();
        properties.load(is);
        String username = properties.getProperty("username");
        String password = properties.getProperty("password");
        resp.getWriter().write(URLDecoder.decode(username,"gbk"));
        resp.getWriter().write(password);
//===========================================================================
//COOKIE
  /*      Cookie cookie=new Cookie("Secrity","121788");
        resp.addCookie(cookie);
        Cookie[] cookies = req.getCookies();
        if (cookies==null){
            resp.getWriter().write("没有cookie");
        }else {
            for (int i = 0; i < cookies.length; i++) {
                Cookie cookie1 = cookies[i];
                if (cookie1.getName().equals("Secrity")) {
                    cookie1.setSecure(true);
                    cookie1.setComment("这是您的密码");
                    resp.getWriter().write(cookie1.getValue()+" "+cookie1.getName()+" "+cookie1.getComment()
                            +" "+cookie1.getPath()+" "+cookie1.getMaxAge()+" "+cookie1.getSecure()+" "+cookie1.getVersion());
                }
            }
        }
   */
//=====================================================================
//        Session
        HttpSession session = req.getSession();
//  1.设置session并且取session
        session.setAttribute("GOODS","密码是:12456");
        String goods = (String) session.getAttribute("GOODS");
        resp.getWriter().write(goods);
//  2.注销指定的session
//        resp.getWriter().write("结束了");
//        session.invalidate();
//        session.removeAttribute("GOODS");
//        resp.getWriter().write(goods);
//  3.获取session创建的时间: 会返回一个时间戳
        long creationTime = session.getCreationTime();
        resp.getWriter().write(creationTime+"");
        Date date = new Date(creationTime);
        resp.getWriter().write(date.toString());
//  4.设置和获取session的有效时间.
//        session.setMaxInactiveInterval(10);
//        resp.getWriter().write(session.getMaxInactiveInterval());
//
//  5.设置ServletContext的属性,以及获取。不能获取不是ServletContext创建的属性
        resp.getWriter().write("你好");
        ServletContext servletContext1 = session.getServletContext();
        servletContext1.setAttribute("he","吉士先生");
        String goods1 = (String) servletContext1.getAttribute("he");
        resp.getWriter().write(goods1);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

(二十)、过滤器、监听器常见应用

1.过滤器注册和注销操作

用户登入之后就可以进入主页,注销之后就不能进入主页

1.创建 login.jsp.里面有一个表单,表单提交后转向LoginServlet.java这个类
的映射。
2.然后映射再找对应的LoginServlet.java,进行判断。再进行重定向。

succes.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>主页</h1>
<p><a href="Servlete/logout" >注销</a></p>
</body>
</html>

login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>吉士先生登入界面</title>
</head>
<body>
<form action="demo4 " method="get">
    用户名<input type="text" name="username" placeholder="请输入您的账号">
    密码<input type="password" name="password" placeholder="请输入您的密码">
    <input type="submit" name="login">
    <input type="reset" name="reset">
</form>
</body>
</html>

error.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>您输入的信息有误</h1>
<p><a href="http://localhost/Maven_Filter_war_exploded/login.jsp" >返回首页</a></p>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <servlet>
        <servlet-name>demo2</servlet-name>
        <servlet-class>Servlet.Demo2</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>demo2</servlet-name>
        <url-pattern>/servlet/show</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>demo2</servlet-name>
        <url-pattern>/servletshow</url-pattern>
    </servlet-mapping>
    <filter>
        <filter-name>demo1</filter-name>
        <filter-class>com.Jsxs.Filter.Demo1</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>demo1</filter-name>
<!--      如果是Servlet下的所有目录,都可以用  -->
        <url-pattern>/succes.jsp</url-pattern>
    </filter-mapping>
<!--   注册监听器 -->
    <listener>
        <listener-class>Listener.OnionCount</listener-class>
    </listener>
    <servlet>
        <servlet-name>LoginServlet</servlet-name>
        <servlet-class>Servlet.LoginServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>LoginServlet</servlet-name>
        <url-pattern>/demo4</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>logout</servlet-name>
        <servlet-class>Servlet.LogOut</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>logout</servlet-name>
        <url-pattern>/Servlete/logout</url-pattern>
    </servlet-mapping>
</web-app>

log.java

package Servlet;
import util.Constanta;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
public class LogOut extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        if (session!=null){
            session.removeAttribute(Constanta.USER_SESSION);
            System.out.println("注销成功");
            resp.sendRedirect("http://localhost/Maven_Filter_war_exploded/login.jsp");
        }
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}


相关文章
|
Java
74.【JavaWeb -02】(四)
74.【JavaWeb -02】
46 0
|
Java Apache C++
74.【JavaWeb -02】(七)
74.【JavaWeb -02】
62 0
|
JavaScript 前端开发 Java
JavaWeb02
JavaWeb02
161 0
|
前端开发 网络协议 Java
javaweb的理解
javaweb的理解
72 0
|
Java
74.【JavaWeb -02】(三)
74.【JavaWeb -02】
48 0
|
Java
74.【JavaWeb -02】(二)
74.【JavaWeb -02】
58 0
|
网络协议 Java Maven
73.【JavaWeb-01】(二)
73.【JavaWeb-01】
56 0
|
前端开发 Java 数据库
75.【JavaWeb-03】(一)
75.【JavaWeb-03】
47 0
|
Java
74.【JavaWeb -02】(五)
74.【JavaWeb -02】
44 0

相关实验场景

更多
下一篇
DataWorks