Servlet是JavaEE的一个组件,也是JavaEE中的一种规范,为了遵循这一规范所有的类都必须实现javax.servlet.Servlet接口
package com.igor.servlet;
import java.io.IOException;
public class ServletDemo extends javax.servlet.http.HttpServlet {
protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
}
protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
//让浏览器打印一句Hello Servlet
response.getWriter().println("Hello Servlet");
}
}
<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>ServletDemoservlet-name>
<servlet-class>com.igor.servlet.ServletDemoservlet-class>
servlet>
<servlet-mapping>
<servlet-name>ServletDemoservlet-name>
<url-pattern>/servleturl-pattern>
servlet-mapping>
web-app>
见:https://blog.csdn.net/weixin_43958460/article/details/96269640 后面的使用IDEA搭建Web开发环境,在搭建完成后运行Tomcat,Tomcat运行后,在运行的初始页面后加上上面web.xml中servlet-apping标签下,url-pattern中的地址,比如:/servlet
此时页面跳转,并显示"Hello Servlet"。
.
在刚下的演示中,我们用一句代码演示了前后端的交互,其中的流程又是怎样的的?
1.一般情况下我们都会指定一个URL , 一个URL对应一个请求
2.通配问题
/* :不报错
*.do
将这个请求,转换到另一个地址;
比如,我们请求 : http://localhost:8080/servlet01/dasdasdasda/aaaa.do 他会跳转到百度首页;
//通过响应对象重定向页面
resp.sendRedirect("404.jsp");
通过this获得ServletContext,实现数据的共享,ServletContext中的资源会被存储到服务器中,所有的Servlet都可以访问的到。
新建一个properties文件
driver=com.mysql.jdbc.Driver
username=root
password=123456
url=jdbc:mysql://localhost:3306/smbms
编写servlet类
package com.kuang.servlet;
import javax.servlet.ServletException;
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.util.Properties;
//读取properties配置文件
public class ServletDemo03 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//读取配置文件
//1.获得配置文件的路径
String realPath = this.getServletContext().getRealPath("/WEB-INF/classes/resources/database.properties");
System.out.println("取得的路径为:"+realPath);
Properties properties = new Properties();
FileInputStream is = new FileInputStream(realPath);
properties.load(is);//把文件流加载到配置文件的对象中;
String driver = properties.getProperty("driver");
String username = properties.getProperty("username");
String password = properties.getProperty("password");
String url = properties.getProperty("url");
//响应到网页
resp.getWriter().println(driver);
resp.getWriter().println(username);
resp.getWriter().println(password);
resp.getWriter().println(url);
//=======================================
System.out.println(driver);
System.out.println(username);
System.out.println(password);
System.out.println(url);
}
}
配置web.xml
<servlet>
<servlet-name>Demo03servlet-name>
<servlet-class>com.kuang.servlet.ServletDemo03servlet-class>
servlet>
<servlet-mapping>
<servlet-name>Demo03servlet-name>
<url-pattern>/q3url-pattern>
servlet-mapping>
访问查看
localhost:8080/demo02/q3
package com.kuang.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.Map;
//验证码
public class ServletDemo04 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//resp.getWriter().println(newRandom());
//自动刷新网页
resp.setHeader("refresh","2");
//验证码是一个图片 , 我们需要制作一个图片
BufferedImage image = new BufferedImage(100,30,BufferedImage.TYPE_3BYTE_BGR);
//图片写入一些东西
Graphics2D graphics = image.createGraphics();
graphics.setColor(Color.red);
String num = String.valueOf(newRandom());
graphics.drawString(num,10,10);
//想办法让浏览器知道我们给的是一张图片
resp.setContentType("image/jpg");
//让网站去打开图片
ImageIO.write(image,"jpg",resp.getOutputStream());
}
//生成随机数
public int newRandom(){
int num = (int)(Math.random()*9+1)*100000;
return num;
}
/*
@Test
public void test(){
ServletDemo04 servletDemo04 = new ServletDemo04();
for (int j = 0; j < 100; j++) {
int i = servletDemo04.newRandom();
System.out.println(i);
}
}
*/
}
${pageContext.request.contextPath}
package com.kuang.servlet;
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 RequestDemo01 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//查看request对象的方式
System.out.println(request.getContextPath()); //获得web项目路径
System.out.println(request.getRequestURI()); //请求的URL路径
//Remote 远程
System.out.println(request.getRemoteUser()); //获得远程的用户名
System.out.println(request.getRequestedSessionId()); //获得SessionId;
System.out.println(request.getServletPath()); //请求servlet的url
System.out.println(request.getLocalAddr()); //获得本地地址 防火墙
System.out.println(request.getLocalName()); //获得本地名字
System.out.println(request.getLocalPort()); //获得访问的端口号
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
写前端注册页面
表单的提交方式 get/post
表单提交的位置 action:服务项目的路径/servlet对应的请求url
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>注册title>
head>
<body>
<%--
JSP的注释
--%>
<%--
Form表单
action:代表要提交的路径,表单要提交到哪里 ,可以提交到一个Servlet
//提交到Servlet需要,获取到Web项目的路径下的Servlet
method: 提交表单的方式
--%>
<h1>注册h1>
<%-----------action的值用的是EL表达式,指跳转到servlet配置的地址----------------%>
<form action="${pageContext.request.contextPath}/r2" method="post">
<p>用户名:<input type="text" name="username">p>
<p>密码:<input type="password" name="password">p>
<p>
<input type="submit">
<input type="reset">
p>
form>
body>
html>
写处理前端提交信息的Servlet
接收前端传递的控件信息, request.getParameter(“控件的name‘);
package com.kuang.servlet;
import javax.print.attribute.HashPrintRequestAttributeSet;
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 RequestDemo02 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//通过HttpServletRequest获取前端用户提交的信息
//req.getParameterValues(); 获得前端传递的控件信息,通过控件的name;但是可以接受多个参数的控件,比如(多选框....)
//req.getParameter(); 获得前端传递的控件信息,通过控件的name;
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
String username = req.getParameter("username");
String password = req.getParameter("password");
//连接数据库,比较数据库中的信息是否匹配
if (username.equals("admin")&&password.equals("1234567")){
System.out.println("登录成功!"); //跳转到成功页面
}else {
System.out.println("登录失败!"); //提示重新注册
}
System.out.println(username);
System.out.println(password);
}
}
配置web.xml
配置对应的XML文件
<servlet>
<servlet-name>RequsetDemo02servlet-name>
<servlet-class>com.kuang.servlet.RequestDemo02servlet-class>
servlet>
<servlet-mapping>
<servlet-name>RequsetDemo02servlet-name>
<url-pattern>/r2url-pattern>
servlet-mapping>
提交测试
package com.kuang.servlet;
import javax.servlet.RequestDispatcher;
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 FormServlet1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//处理乱码
req.setCharacterEncoding("UTF-8");
//处理前端的请求
String username = req.getParameter("username");
String password = req.getParameter("password");
System.out.println("接收到的用户名:"+username+"\n密码:"+password+"\n爱好:");
String[] hobbies = req.getParameterValues("hobby");
for (String hobby : hobbies) {
System.out.println(hobby);
}
//重定向:服务器告诉客户端你应该请求另外一个地址; (地址栏会变)
//resp.sendRedirect("url");
//转发:服务器把这个请求转向另外一个Servlet去处理; (地址栏不会变)
//RequestDispatcher ,需要使用RequestDispatcher来进行处理,我们需要获得这个类
//参数就是他要转发到的页面
/*
RequestDispatcher requestDispatcher = req.getRequestDispatcher("/success.jsp");
requestDispatcher.forward(req,resp);
*/
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
req.getRequestDispatcher("/success.jsp").forward(req,resp);
}
}
重定向:服务器告诉客户端,你去请求另外一个地址;客户端行为
地址栏会变
不能携带参数
转发:服务器自己转发到服务器上的另一个请求; 服务器行为
会话字面意思:一次交流或交谈
在Web中,会话表示从浏览器打开某个网站,在这个网站中无论操作了什么,知道关闭浏览器,这一个过程,称之为一个会话。
怎么样算会话结束:
为什么要处理会话:
长期保持会话,无论用户关闭少次浏览器,这个会话都要存在;
比喻:
你昨天来了教室,我今天需要知道你昨天来了教室;
你:客户端 我 :服务器
Javaweb中针对这两个不同的端,诞生两个小机制
cookie :在客户端留下一点东西,客户端下次带过来,我就知道你来过了。
比如说:我的店铺非常大,怎么证明你来过?
你开一个会员吧 , 我给你一张VIP卡,你下次带卡来,我就知道你来过了
Session:在服务器端登记你来过。
比如说:你去理发店。怎么证明你来过?
理发店收会员,在他的笔记本上记录了你的消息,下次你来直接报上大名就可以了。
构造器 :
Cookie cookie = new Cookie(String name,String value);
Cookie cookie = new Cookie(String name,String value);
Cookie cookie = new Cookie(String name,String value);
服务器响应cookie给客户端
Response.addCookie(Cookie);
Response.addCookie(Cookie);
Response.addCookie(Cookie);
服务查看用户带来的请求是否有cookie
Cookie[] cookies = Request.getCookie();
//可以使用cookie来验证用户是否来过
//判断cookies是否为空,然后遍历即可
Cookie.getName();
Cookie.getValue();
package com.westos.serlvet;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class CookieTest extends HttpServlet {
boolean flag = false;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//解决乱码问题:
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
//检查请求的人是否带了Cookie
//通过用户的请求,检查它是否有cookie
Cookie[] cookies = request.getCookies();
System.out.println("cookie:"+cookies);
if (flag){//如果flag是真,来过
if (cookies!=null){//你有cookie
for (int i = 0; i < cookies.length ; i++) {
Cookie cookie = cookies[i];
if (cookie.getName().equals("lastLoginTime")){
response.getWriter().println("你上一次来的时间为:"+cookie.getValue());
System.out.println("刷新了cookie");
}
}
}
}else { //如果flag是真,没来过
response.getWriter().println("你是第一次来,还想要会员套餐");
System.out.println("给该用户一个cookie");
}
//建立一个cookie
Cookie cookie = new Cookie("lastLoginTime",System.currentTimeMillis()+"");
//设置cookie保存的时间 //cookie.setMaxAge();
//把这个cookie发给客户端
response.addCookie(cookie);
//response.addCookie(new Cookie("lastLoginTime",System.currentTimeMillis()+""));
flag = true;
}
}
只要客户端一旦连接上服务器,服务器就会自动产生Session;
一个连接对应一个session,session可以在一个会话中传递信息;
通过setAttribute设置值。
通过getAttribute获得值。
由服务器端控制,服务器如果重启了,信息就会丢失!
package com.westos.serlvet;
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 SessionTest extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//解决乱码问题:
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
//Session由服务器自动创建,如何获得session
//HttpSession 得到的sessioin对象
HttpSession session = req.getSession();
//得到sessionID,一次会话,一个seesionID;
String id = session.getId();
resp.getWriter().println("获得的sessionId:\n"+id);
String name = "qinjiang";
//向session中存入一个值;
session.setAttribute("name",name);
resp.getWriter().println("存入信息成功:"+name);
}
}
package com.westos.serlvet;
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;
//获得session存入的值
public class SessionTest2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//解决乱码问题:
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
//获得
HttpSession session = req.getSession();
System.out.println("得到的SessionID:"+session.getId());
String name = (String) session.getAttribute("name");
resp.getWriter().println("得到的session存入的信息"+name);
//Servlet < Seesion < WebContext
}
}
会话注销方法一:
session.invalidate();//通过代码注销会话
会话注销方法二:
<session-config>
<session-timeout>1session-timeout>
session-config>