可以配置启动的端口号:默认是8080
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
可以配置主机启动的名称:默认的注解名为localhost->127.0.0.1
默认网站应用存放的位置为:webapps
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
网站是如何进行访问的:
客户端——发请求(Request)——服务器
百度:
Request URL: https://www.baidu.com/ 请求地址
Request Method: GET get方法/post方法
Status Code: 200 OK 状态码:200
Remote Address: 182.61.200.7:443
Referrer(远程) Policy: strict-origin-when-cross-origin
Accept: text/htmlapplication/signed-exchange;v=b3;q=0.9
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9 语言
Cache-Control: max-age=0
Connection: keep-alive
Cookie: BIDUPSID=D733CC6C26608FED3DECDAE23B8DE55C; PSTM=1647917034; BAIDUID=D733CC6C26608FEDE84F1F68645CADBA:FG=1; BD_UPN=12314753; BDSFRCVID_BFESS=OR-OJexroG0CZ3oDk5-N2ru_CgKK0gOTDYrEOwXPsp3LGJLVcf13EG0PtfOEtU8MaXv6ogKKWeOTHx8F_2uxOjjg8UtVJeC6EG0Ptf8g0M5; H_BDCLCKID_SF_BFESS=tb4tVCIafII3jtOY5-__-4_tbh_X5-RLf26xVp7F5l8-h45uDJ5Fjx332-7v-4omWa7MblQVyJcxOKQeDUrqMntlXl6EKlOXaHTO3RbN3KJmhlC9bT3v5tDW-UJe2-biWav-2MbdJqvP_IoG2Mn8M4bb3qOpBtQmJeTxoUJ25DnJhbLGe6-Bej5yjaAqq-jeHDrKBRbaHJOoDDvsjfbcy4LbKxnxJUJztKORBKKEaDoKff7JbURvD--g3-OkbfQ9babTQ-tbBp3k8MQnb6L5QfbQ0hOhWMQJQRbu3xnKJJ7JOpv5bfnxybvbQRPH-Rv92DQMVU52QqcqEIQHQT3m5-5bbN3ut6IefRKqoK-yJDvDqTrP-trf5DCShUFsWhDLB2Q-XPoO3KJToCOvQ-4hqf6XKlod3-biWbRM2Mbgy4op8P3y0bb2DUA1y4vp5-KDt2TxoUJ25q7pfMK9XtQi56KebPRiL-Q9QgbOVpQ7tt5W8ncFbT7l5hKpbt-q0x-jLTnhVn0MBCK0HPonHjD5DTcy3j; ZFY=YSqfBkC0:B5ioEC90tsGe:BQDSEYHC3:BI9jHB2PbOuYTU:C; BAIDUID_BFESS=CDDFE4691243C3E75428BCCC3B72A945:FG=1; baikeVisitId=82be6391-cccb-45ea-995b-75caf7dd760d; B64_BOT=1; BA_HECTOR=84ag2481ag240l00011hc7td515; COOKIE_SESSION=246325_0_8_8_11_8_1_0_8_8_0_0_246364_0_68_0_1657009641_0_1657009573%7C9%23331664_53_1655720451%7C9; BDRCVFR[n9IS1zhFc9f]=mk3SLVN4HKm; delPer=0; BD_CK_SAM=1; PSINO=1; ab_sr=1.0.1_ZWI4NGRlOWEzMzYzYWM0ZDk0MzJkNGY2ZGEzN2Y0MmRiMzhhMzM3NjlmMmQ5MGFiYjdmNjlmMWIyM2Q4NzVhZjkzYWQzYjA0YTA2OTQwMmU5Y2M2MjkwYjU3ZTU0N2M2ZjVhMjFmNWEyNDhkODk0ZDI4OWJmYTc0MmIxM2Y1NjkyMzE5M2ZjMDA5ZjNhZDgxMGZlZjViYTc2MGFiOWU2Zg==; BD_HOME=1; H_PS_PSSID=36544_36757_36726_36454_31253_36413_36690_36167_36694_36696_36569_36777_36775_36745_36762_36768_36765_26350_36711_36649; BDRCVFR[S4-dAuiWMmn]=I67x6TjHwwYf0; H_PS_645EC=4e80A460UGYdLhDqr8h090BJbOKecLNdgYFGXvBtBmGT9AyGDR3duV4gAyL1DtxWVQ; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598
Host: www.baidu.com
sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="100", "Google Chrome";v="100"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
Sec-Fetch-Dest: document
Sec-Fetch-Mode: navigate
Sec-Fetch-Site: none
Sec-Fetch-User: ?1
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36
请求行:请求好的请求方式:GET;请求方式:Get,Post,Delete,Put···
消息头:
Accept: 告诉服务器,它所支持的数据类型
Accept-Encoding: 支持那种编码格式
Accept-Language: 告诉服务器,它的语言环境
Cache-Control: 缓存控制
Connection: 告诉服务器,请求完成时断开还是保持连接
Host: 主机
···
服务器——响应——客户端
百度:
Connection: keep-alive 连接
Content-Encoding: gzip 编码
Content-Type: text/html; charset=utf-8 类型
Accept: 告诉服务器,它所支持的数据类型
Accept-Encoding: 支持那种编码格式
Accept-Language: 告诉服务器,它的语言环境
Cache-Control: 缓存控制
Connection: 告诉服务器,请求完成时断开还是保持连接
Host: 主机
···
Refresh: 告诉客户端,多久刷新一次
Location: 让网页重新定位
为什么要学习这个技术
- 在JavaWeb开发中,需要使用大量的jar包,我们手动去导入
- 如何能够让一个东西自动帮我们导入和配置这个jar包,因此,Maven诞生了
约定大于配置
<mirror>
<id>aliyunmavenid>
<mirrorOf>*mirrorOf>
<name>阿里云公共仓库name>
<url>https://maven.aliyun.com/repository/publicurl>
mirror>
<localRepository>E:\Practical software\Java\Maven\maven-repolocalRepository>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>org.examplegroupId>
<artifactId>javaweb-01-mavenartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>warpackaging>
<name>javaweb-01-maven Maven Webappname>
<url>http://www.example.comurl>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.7maven.compiler.source>
<maven.compiler.target>1.7maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
<scope>testscope>
dependency>
dependencies>
<build>
<finalName>javaweb-01-mavenfinalName>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-pluginartifactId>
<version>3.1.0version>
plugin>
<plugin>
<artifactId>maven-resources-pluginartifactId>
<version>3.0.2version>
plugin>
<plugin>
<artifactId>maven-compiler-pluginartifactId>
<version>3.8.0version>
plugin>
<plugin>
<artifactId>maven-surefire-pluginartifactId>
<version>2.22.1version>
plugin>
<plugin>
<artifactId>maven-war-pluginartifactId>
<version>3.2.2version>
plugin>
<plugin>
<artifactId>maven-install-pluginartifactId>
<version>2.5.2version>
plugin>
<plugin>
<artifactId>maven-deploy-pluginartifactId>
<version>2.8.2version>
plugin>
plugins>
pluginManagement>
build>
project>
Maven由于他的约定大于配置,我们之后可能遇到我们写的配置文件,无法被导出或者生效的问题,解决方案:
<build>
<resources>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
includes>
<filtering>falsefiltering>
resource>
<resource>
<directory>src/main/resourcesdirectory>
<includes>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
includes>
<filtering>falsefiltering>
resource>
resources>
build>
Servlet
Servlet接口Sun公式有两个默认的实现类:HttpServlet,
构建一个普通的Maven项目,删除src目录。以后在项目建立Model。这个空的工程就是Maven的主工程
导入所需的依赖:
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>4.0.1version>
dependency>
<dependency>
<groupId>javax.servlet.jspgroupId>
<artifactId>javax.servlet.jsp-apiartifactId>
<version>2.3.3version>
dependency>
关于Maven父子工程的理解:
<modules>
<module>Servlet01module>
modules>
<parent>
<artifactId>javaweb-02-servletartifactId>
<groupId>org.examplegroupId>
<version>1.0-SNAPSHOTversion>
parent>
son extends father
Maven环境优化
编写一个Servlet
package com.javastudy.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.io.PrintWriter;
public class HelloServlet extends HttpServlet {
//由于get或者post只是请求实现的不同方式,可以相互调用,业务逻辑都一样
@Override
protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
PrintWriter writer = resp.getWriter();//响应流
writer.print("Hello Servlet");
}
@Override
protected void doPost( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
}
}
编写Servlet的映射:
为什么需要映射:我们写的是Java程序,但是要通过浏览器访问,而浏览器需要连接web服务器,所以我们需要在web服务器中注册我们写的Servlet,还需要给它一个浏览器能够访问的路径
xml文件:
<servlet>
<servlet-name>Helloservlet-name>
<servlet-class>com.javastudy.servlet.HelloServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>Helloservlet-name>
<url-pattern>url-pattern>
servlet-mapping>
HelloServlet类
package com.javastudy.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.io.PrintWriter;
public class HelloServlet extends HttpServlet {
//由于get或者post只是请求实现的不同方式,可以相互调用,业务逻辑都一样
@Override
protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
System.out.println("进入doGet方法");
PrintWriter writer = resp.getWriter();//响应流
writer.print("Hello Servlet");
}
@Override
protected void doPost( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
}
}
配置Tomcat:
启动测试:
一个Servlet可以指定一个映射路径:
<servlet-mapping>
<servlet-name>Helloservlet-name>
<url-pattern>/Hellourl-pattern>
servlet-mapping>
一个Servlet可以指定多个映射路径:
<servlet-mapping>
<servlet-name>Helloservlet-name>
<url-pattern>/Hellourl-pattern>
servlet-mapping>
<servlet-mapping>
<servlet-name>Helloservlet-name>
<url-pattern>/Hello1url-pattern>
servlet-mapping>
<servlet-mapping>
<servlet-name>Helloservlet-name>
<url-pattern>/Hello2url-pattern>
servlet-mapping>
一个Servlet可以指定通用映射路径:
<servlet-mapping>
<servlet-name>Helloservlet-name>
<url-pattern>/Hello/*url-pattern>
servlet-mapping>
默认请求路径:
<servlet-mapping>
<servlet-name>Helloservlet-name>
<url-pattern>/*url-pattern>
servlet-mapping>
指定一些后缀或者前缀等等···
<servlet-mapping>
<servlet-name>Helloservlet-name>
<url-pattern>*.javastudyurl-pattern>
servlet-mapping>
优先级问题:
<servlet>
<servlet-name>errorservlet-name>
<servlet-class>com.javastudy.servlet.ErrorServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>errorservlet-name>
<url-pattern>/*url-pattern>
servlet-mapping>
web容器在启动的时候,它会为每个web程序都创建一个对应的ServletContext对象,它代表当前的web应用
放置数据的类:
package com.javastudy.servlet;
import javax.servlet.ServletContext;
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 HelloServlet extends HttpServlet {
@Override
protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
//this.getInitParameterNames() 初始化参数
//this.getServletConfig() Servlet配置
//this.getServletContext() Servlet上下文
ServletContext context = this.getServletContext();
String username = "Java学不会"; //数据
context.setAttribute("username",username); //将一个数据保存宰了Servlet中,名字为:username 值为:username
System.out.println("Hello");
}
}
读取数据的类:
package com.javastudy.servlet;
import javax.servlet.ServletContext;
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 GetServlet extends HttpServlet {
@Override
protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException, IOException {
ServletContext context = this.getServletContext();
String username =(String) context.getAttribute("username");
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
resp.getWriter().println("名字:"+username);
}
}
web.xml:
<servlet>
<servlet-name>helloservlet-name>
<servlet-class>com.javastudy.servlet.HelloServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>helloservlet-name>
<url-pattern>/hellourl-pattern>
servlet-mapping>
<servlet>
<servlet-name>getservlet-name>
<servlet-class>com.javastudy.servlet.GetServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>getservlet-name>
<url-pattern>/geturl-pattern>
servlet-mapping>
测试结果:
web.xml文件:
<context-param>
<param-name>urlparam-name>
<param-value>E:java/studyparam-value>
context-param>
<servlet>
<servlet-name>urlservlet-name>
<servlet-class>com.javastudy.servlet.ServletDemo03servlet-class>
servlet>
<servlet-mapping>
<servlet-name>urlservlet-name>
<url-pattern>/urlurl-pattern>
servlet-mapping>
获取初始化内容:
protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String url = context.getInitParameter("url");
resp.getWriter().println(url);
}
web.xml文件:
<servlet-mapping>
<servlet-name>getservlet-name>
<url-pattern>/geturl-pattern>
servlet-mapping>
<servlet>
<servlet-name>urlservlet-name>
<servlet-class>com.javastudy.servlet.ServletDemo03servlet-class>
servlet>
<servlet-mapping>
<servlet-name>urlservlet-name>
<url-pattern>/urlurl-pattern>
servlet-mapping>
<servlet>
<servlet-name>sd4servlet-name>
<servlet-class>com.javastudy.servlet.ServletDemo04servlet-class>
servlet>
<servlet-mapping>
<servlet-name>sd4servlet-name>
<url-pattern>/sd4url-pattern>
servlet-mapping>
实现代码:
protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
ServletContext context = this.getServletContext();
System.out.println("进入了ServletDemo04");
/*RequestDispatcher requestDispatcher = context.getRequestDispatcher("/gp"); //转发的请求路径
requestDispatcher.forward(req,resp); //调用forward实现请求转发*/
context.getRequestDispatcher("/url").forward(req, resp);
}
在Java目录下新建properties
-*在resources目录下新建properties
发现:都被打包到了同一个路径下:classes,我们俗称这个路径为classpath
需要一个文件流
protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/com/javastudy/servlet/aa.properties");
Properties prop = new Properties();
prop.load(is);
String user = prop.getProperty("username");
String pwd = prop.getProperty("password");
resp.getWriter().print(user+":"+pwd);
}
负责向浏览器发送数据的方法:
public ServletOutputStream getOutputStream() throws IOException;
public PrintWriter getWriter() throws IOException;
负责向浏览器发送数据的方法:
public void setCharacterEncoding(String charset);
public void setContentLength(int len);
public void setContentLengthLong(long len)
public void setContentType(String type);
public void setDateHeader(String name, long date);
public void addDateHeader(String name, long date);
public void setHeader(String name, String value);
public void addHeader(String name, String value);
public void setIntHeader(String name, int value);
响应的状态码:
public static final int SC_CONTINUE = 100;
public static final int SC_SWITCHING_PROTOCOLS = 101;
public static final int SC_CREATED = 201;
public static final int SC_ACCEPTED = 202;
public static final int SC_NON_AUTHORITATIVE_INFORMATION = 203;
public static final int SC_NO_CONTENT = 204;
public static final int SC_RESET_CONTENT = 205;
public static final int SC_PARTIAL_CONTENT = 206;
public static final int SC_MULTIPLE_CHOICES = 300;
public static final int SC_MOVED_PERMANENTLY = 301;
public static final int SC_MOVED_TEMPORARILY = 302;
public static final int SC_FOUND = 302;
public static final int SC_SEE_OTHER = 303;
public static final int SC_NOT_MODIFIED = 304;
public static final int SC_USE_PROXY = 305;
public static final int SC_TEMPORARY_REDIRECT = 307;
public static final int SC_BAD_REQUEST = 400;
public static final int SC_UNAUTHORIZED = 401;
public static final int SC_PAYMENT_REQUIRED = 402;
public static final int SC_FORBIDDEN = 403;
public static final int SC_NOT_FOUND = 404;
public static final int SC_METHOD_NOT_ALLOWED = 405;
public static final int SC_NOT_ACCEPTABLE = 406;
public static final int SC_PROXY_AUTHENTICATION_REQUIRED = 407;
public static final int SC_REQUEST_TIMEOUT = 408;
public static final int SC_CONFLICT = 409;
public static final int SC_GONE = 410;
public static final int SC_LENGTH_REQUIRED = 411;
public static final int SC_PRECONDITION_FAILED = 412;
public static final int SC_REQUEST_ENTITY_TOO_LARGE = 413;
public static final int SC_REQUEST_URI_TOO_LONG = 414;
public static final int SC_UNSUPPORTED_MEDIA_TYPE = 415;
public static final int SC_REQUESTED_RANGE_NOT_SATISFIABLE = 416;
public static final int SC_EXPECTATION_FAILED = 417;
public static final int SC_INTERNAL_SERVER_ERROR = 500;
public static final int SC_NOT_IMPLEMENTED = 501;
public static final int SC_BAD_GATEWAY = 502;
public static final int SC_SERVICE_UNAVAILABLE = 503;
public static final int SC_GATEWAY_TIMEOUT = 504;
public static final int SC_HTTP_VERSION_NOT_SUPPORTED = 505;
package com.javastudy.servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
public class FileServlet extends HttpServlet {
@Override
protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
//获取下载文件的路径
String realPath = "E:\\Download\\java\\javaweb-02-servlet\\response\\target\\classes\\Java学不会.png";
System.out.println("下载文件的路径:"+realPath);
//下载的文件名
String fileName = realPath.substring(realPath.lastIndexOf("\\")+1);
//设置浏览器能够支持下载需要的东西,中文文件名RLEncoder.encode编码
resp.setHeader("Content-disposition","attachment;filename="+ URLEncoder.encode(fileName,"UTF-8"));
//获取下载文件的输入流
FileInputStream in = new FileInputStream(realPath);
//创建缓冲区
int len = 0;
byte[] buffer = new byte[1024];
//获取inpuutStream对象
ServletOutputStream out = resp.getOutputStream();
//将FileinputStream流写入到buffer缓冲区
while((len=in.read(buffer))!=-1){
out.write(buffer,0,len);
}
out.close();
in.close();
//使用OutputStream将缓冲区中的数据输出到客户端
}
@Override
protected void doPost( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
}
}
package com.javastudy.servlet;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
public class ImageServlet extends HttpServlet {
@Override
protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
//如何浏览器5买走自动刷新一次;
resp.setHeader("refresh","5");
//在内存中创建一个图片
BufferedImage image = new BufferedImage(80,20, BufferedImage.TYPE_INT_RGB);
//得到图片
Graphics2D g = (Graphics2D) image.getGraphics();
//设置图片的背景颜色
g.setColor(Color.WHITE);
g.fillRect(0,0,80,20);
//给图片写数据
g.setColor(Color.BLUE);
g.setFont(new Font("微软雅黑",Font.BOLD,20));
g.drawString(makeNume(),0,20);
//高数浏览器,这个请求用图片的方式打开
resp.setContentType("image/png");
//网站存在缓存,不让浏览器缓存
resp.setDateHeader("expires",-1);
resp.setHeader("Cache-Control","no-Cache");
resp.setHeader("Pragma","no-cache");
//把图片写个浏览器
ImageIO.write(image,"png",resp.getOutputStream());
}
//生成随机数
private String makeNume(){
Random random = new Random();
String num = random.nextInt(9999999) + "";
StringBuffer sb = new StringBuffer();
for (int i = 0; i < 7-num.length(); i++) {
sb.append("0");
}
num = sb.toString() + num;
return num;
}
@Override
protected void doPost( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
}
}
Web资源收到客户端请求后,会通知客户端去访问另外一个Web资源,这个过程就叫重定向
@Override
protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
/*resp.setHeader("Location","/response_war/image");
resp.setStatus(302);*/
resp.sendRedirect("/response_war/image");//重定向
}
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录title>
head>
<body>
<h1>登录h1>
<div style="text-align: center">
<%--这里表单表示:以post方式提交表单,提交到我们的login请求--%>
<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>
div>
body>
html>
LoginServlet
package com.javastudy.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 LoginServlet extends HttpServlet {
@Override
protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
String username = req.getParameter("username");
String password = req.getParameter("password");
String[] hobbys = req.getParameterValues("hobbys");
System.out.println("======================");
System.out.println(username);
System.out.println(password);
System.out.println(Arrays.toString(hobbys));
System.out.println("======================");
//这里/代表当前web应用
req.getRequestDispatcher("/success.jsp").forward(req,resp);
}
@Override
protected void doPost( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
doGet(req,resp);
}
}
有状态会话
(cookie)
(seesion)
客户端技术(响应,请求)
从请求中拿到cookie信息
服务器响应给客户端cookie
Cookie[] cookies = req.getCookies(); //获得cookie
cookie.getName(); //获得cookie中的key
cookie.getValue(); //获得cookie中的value
Cookie cookie = new Cookie("lastLoginTime", System.currentTimeMillis()+""); //新建一个cookie
cookie.setMaxAge(24*60*60); //设置cookie的有效期
resp.addCookie(cookie); //响应个客户端一个cookie
cookie:一般会保存在本地的 用户目录下 appdata
删除cookie
服务器技术,利用这个技术,可以保存用户的会话信息。我们可以把信息或者数据放在session中
常见场景:网站登录之后,你下次不用再登录了,第二次访问直接就上去了!
Cookie是把用户的数据写给用户的浏览器,浏览器保存(可以保存多个)
Session把用户的数据写到用户独占的Session中,服务器端保存(保存重要的信息,减少服务器资源的浪费)
Session对象由服务器创建
使用session
public class SessionDemo01 extends HttpServlet {
@Override
protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
//解决乱码问题
resp.setCharacterEncoding("UTF-8");
req.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=utf-8");
//得到Session
HttpSession session = req.getSession();
//给Session中存放东西
session.setAttribute("name",new Person("Java学不会",2));
//获取Session的ID
String id = session.getId();
//判断Session是不是新建的
if(session.isNew()){
resp.getWriter().write("session创建成功:ID:"+id);
}else{
resp.getWriter().write("session已经在服务器中存在了:ID:"+id);
}
}
@Override
protected void doPost( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
super.doPost(req, resp);
}
}
注销session
public class SessionDemo03 extends HttpServlet {
@Override
protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
HttpSession session = req.getSession();
session.removeAttribute("name");
session.invalidate();
}
@Override
protected void doPost( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
super.doPost(req, resp);
}
}
自动注销
<!-- 设置session默认的失效时间-->
<session-config>
<!-- 单位为分钟-->
<session-timeout>1</session-timeout>
</session-config>
JSP最终也会被转换为一个Java类
JSP本质上就是一个Servelet
//初始化
public void _jspInit() {
}
//注销
public void _jspDestroy() {
}
//JSPService
public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
判断请求
内置一些对象
final javax.servlet.jsp.PageContext pageContext; //页面上下文
javax.servlet.http.HttpSession session = null; //session
final javax.servlet.ServletContext application;
final javax.servlet.ServletConfig config; //配置
javax.servlet.jsp.JspWriter out = null;
final java.lang.Object page = this; //page:当前
HttpServletRequest request //请求
HttpServletResponse response //响应
输出页面前增加的代码
response.setContentType("text/html"); //设置响应的页面类型
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;
PageContext 存东西
Request 存东西
Response
Session 存东西
Application (ServletContext) 存东西
config (servletConfig)
out
page
exception
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%-- 内置对象--%>
<%
pageContext.setAttribute("name1","Java学不会1");//保存的数据只在一个页面中有效
request.setAttribute("name2","Java学不会2");//保存的数据只在一次请求有效,请求转发会携带这个数据
session.setAttribute("name3","Java学不会3");//保存的数据只在一次会话有效,从打开浏览器到关闭浏览器
application.setAttribute("name4","Java学不会4");//保存的数据只在服务器中有效,从打开服务器到关闭服务器
%>
<%--脚本判断中的代码,会被原封不动的生成到.jsp.java
要求:必须保存Java语法的正确性--%>
<%
//从pageContext取出,通过寻找方式来
//从底层到高层(作用域)
String name1 = (String) pageContext.findAttribute("name1");
String name2 = (String) pageContext.findAttribute("name2");
String name3 = (String) pageContext.findAttribute("name3");
String name4 = (String) pageContext.findAttribute("name4");
%>
<%--使用El表达式输出 ${}--%>
<h1>取出的值为:</h1>
<h3>${name1}</h3>
<h3>${name2}</h3>
<h3>${name3}</h3>
<h3>${name4}</h3>
</body>
</html>
依赖包
<!--JSTL依赖-->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<!--standard依赖-->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
EL表达式:${}
JSP标签 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<jsp:forward page="jsptag2.jsp">
<jsp:param name="name" value="Java学不会"/>
<jsp:param name="age" value="2023"/>
</jsp:forward>
JSTL表达式
<body>
<form action="coreif.jsp" method="get">
<input type="text" name="username" value="${param.username}">
<input type="submit" value="登录">
</form>
<%--判断如果提交的用户名是管理员,则登录成功--%>
<c:if test="${param.username=='admin'}" var="isAdmin" >
<c:out value=" 管理员欢迎你">
</c:out>
<c:out value="${isAdmin}"></c:out>
</c:if>
</body>
<body>
<c:set var="score" value="85"></c:set>
<c:choose>
<c:when test="${score>=90}">你的成绩为优秀</c:when>
<c:when test="${score>=80}">你的成绩为良好</c:when>
<c:when test="${score>=70}">你的成绩为不错</c:when>
</c:choose>
</body>
<body>
<%
ArrayList<String> people = new ArrayList<>();
people.add("张三");
people.add("李四");
people.add("王五");
people.add("赵六");
people.add("田七");
request.setAttribute("list",people);
%>
<%--var,每一遍历出来的变量
items,要遍历的对象--%>
<c:forEach var="people" items="${list}">
<c:out value="${people}">
</c:out>
</c:forEach>
</body>
编写代码
public class CharacterEncodingFilter implements Filter {
@Override
//初始化
public void init( FilterConfig filterConfig ) throws ServletException {
System.out.println("初始化");
}
@Override
//Chain:链
/*
1.过滤中的所有代码,在过滤特定请求的时候都会执行
2.必须要让过滤器继续同行 chain.doFilter(request,response);
*/
public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain ) throws IOException, ServletException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=UTF-8");
System.out.println("执行前");
chain.doFilter(request,response); //让请求继续走,如果不写,程序到这里就被拦截停止
System.out.println("执行后");
}
@Override
//销毁,web服务器关闭过滤器销毁
public void destroy( ) {
System.out.println("销毁");
}
}
在web.xml中配置Filter
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>com.javastudy.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<!--只要是/servlet的任何请求都会经过这个过滤器-->
<url-pattern>/servlet/*
//统计网站在线人数:统计session
public class OnlineCountListener implements HttpSessionListener {
@Override
//创建session监听
public void sessionCreated( HttpSessionEvent se ) {
ServletContext ctx = se.getSession().getServletContext();
System.out.println(se.getSession().getId());
Integer onlineCount = (Integer) ctx.getAttribute("OnlineCount");
if(onlineCount==null){
onlineCount=new Integer(1);
}else{
int count =onlineCount.intValue();
onlineCount=new Integer(count+1);
}
ctx.setAttribute("OnlineCount",onlineCount);
}
@Override
//销毁session监听
public void sessionDestroyed( HttpSessionEvent se ) {
ServletContext ctx = se.getSession().getServletContext();
Integer onlineCount = (Integer) ctx.getAttribute("OnlineCount");
se.getSession().invalidate();
if(onlineCount==null){
onlineCount=new Integer(0);
}else{
int count =onlineCount.intValue();
onlineCount=new Integer(count-1);
}
ctx.setAttribute("OnlineCount",onlineCount);
}
/*
session销毁:
1.手动销毁
2.自动销毁
*/
}
web.xml中注册监听器
<!--注册监听器-->
<listener>
<listener-class>com.javastudy.listener.OnlineCountListener</listener-class>
</listener>
依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
@Test注解只有在方法上有效,只要加了个注解的方法,就可以直接运行
@Test
public void hello(){
System.out.println("hello");
}