Servlet:
概念:
是Java Servlet的简称,称为小服务程序或服务连接器,用Java编写的服务器端程序,具有独立于
平台和协议的特性,主要功能在于交互式地浏览和生成数据,生成 动态Web内容。这个过程为:
1.客户端发送请求至服务器端
2.服务器将请求信息发送至 Servlet
3.Servlet 生成响应内容并将其传给服务器。响应内容动态生成,通常取决于客户端的请求
4.服务器将响应返回给客户端
Servlet 看起来像是通常的 Java 程序。Servlet 需要导入特定的属于 Java Servlet API 的包。
作用:
1.servlet接收浏览器发送的请求,接收请求数据
2.servlet对请求处理,然后响应数据给浏览器
实现方式:
Servlet有三种实现方式:实现Servlet接口, 继承抽象类GenericServlet, 继承HttpServlet
步骤一:idea创建一个web项目
步骤二:创建包
步骤三:创建类实现接口servlet
步骤四:在类里配置访问路径
步骤五:浏览器测试
package cn.tedu.web;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(urlPatterns="/demo1")
public class Demo1Servlet implements Servlet {
@Override
public void init(ServletConfig servletConfig) throws ServletException {
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public void service(ServletRequest servletRequest, ServletResponse Response) throws ServletException, IOException {
System.out.println("hello");
PrintWriter writer=Response.getWriter();
writer.write("hello response");
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
}
}
总结:servlet运行在tomcat服务器上,保存在tomcat服务器内存中,且只创建一次
init()方法执行一次,service()执行多次,每次访问都会执行,destroy()执行一次
关闭时执行
优化:
自定义类,继承httpservlet,重写以下两个方法
doGet(请求对象request,响应对象response)处理浏览器提交的get请求
doPost(请求对象request,响应对象response)处理浏览器提交的post请求
post请求和get请求提交中文时,处理方式不一样,所以在实际开发中,在继承httpservlet时,不重写service,而是重写doGet()和doPost()
package cn.tedu.web;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(urlPatterns = "/emo5")
public class Demo5Servlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("get---");
//写入浏览器内容
resp.getWriter().write("get aaaa");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("post---");
resp.getWriter().write("post aaaa");
}
}
package cn.tedu.web;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(urlPatterns = "/emo5")
public class Demo5Servlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("get---");
//写入浏览器内容
resp.getWriter().write("get aaaa");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("post---");
resp.getWriter().write("post aaaa");
}
}
热加载:热加载后用debug启动
浏览器中获取数据,从html文件中的name属性获取
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="a" method="get">
用户名:<input type="text" name="username">
<br/>
密 码:<input type="password" name="psd">
<br/>
爱 好:
<input type="checkbox" name="love" value="1">爱评论
<input type="checkbox" name="love" value="2">爱学习
<input type="checkbox" name="love" value="3">拿高薪
<br/>
<input type="submit" value="提交数据">
</form>
</body>
</html>
Request:
获取单个属性
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
@WebServlet(urlPatterns = "/a")
public class ParamServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String n=request.getParameter("username");
System.out.println("请求的用户名:"+n);
String p=request.getParameter("psd");
System.out.println("请求的密码:"+p);
String[] l=request.getParameterValues("love");
System.out.println("请求的爱好:"+ Arrays.toString(l));
}
}
获取全部属性
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;
@WebServlet(urlPatterns= "/Servlet")
public class Servlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Map<String,String[]> map=request.getParameterMap();
Set<String> s=map.keySet();
//高效for循环快捷方式:iter
for (String s1 : s) {
System.out.println(s1+":"+ Arrays.toString(map.get(s1)));
}
}
}
getpost()获取数据乱码问题
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(urlPatterns = "/a")
public class ParamServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String n=request.getParameter("username");
System.out.println("请求的用户名:"+n);
}
}
Response
字节流处理乱码
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(urlPatterns = "/code1")
public class Demo1Servlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
resp.getWriter().write("你好字符流");
}
}
字符流处理乱码
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(urlPatterns = "/code1")
public class Demo1Servlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置响应对象的编码格式
resp.setContentType("text/html;charset=utf-8");
String str="你好";
//设置字节流中的编码格式
byte[] bytes = str.getBytes("utf-8");
resp.getOutputStream().write(bytes);
}
}
重定向方法
重定向:从一个资源跳转到另一个资源(比如:点击超链接跳转)
站外跳转:从自己的网站重定向到另一个网站
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/outer")
public class ServletRedirect extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//处理响应中文的乱码问题
response.setContentType("test/html,charset=utf-8");
//重定向:绝对路径
String OuterUrl="http://www.baidu.com";
response.sendRedirect(OuterUrl);
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<a href="outer">点击我</a>
</body>
</html>
练习:前后端登录
后端:
package comm.tedu.web;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(urlPatterns= "/log")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//处理请求中文乱码的问题
request.setCharacterEncoding("utf-8");
//处理响应中文乱码的问题
response.setContentType("text/html;charset=utf-8");
//获取用户数据
String user = request.getParameter("username");
String psw = request.getParameter("psw");
if(user.equals("admin")&&psw.equals("111")){
String url="http://localhost:8080/index.html";
response.sendRedirect(url);
}else {
String info="登陆失败";
String url="http://localhost:8080/login.html";
response.getWriter().write(info+"<br/>"+"<a href='"+url+"'>去登陆</a>");
}
}
}
前端代码:
index.html文件:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>欢迎你访问</h1>
</body>
</html>
login.html文件:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>login pages</title>
</head>
<body>
<div style="align-content: center">
<form action="log" method="post">
用户名:<input type="text" name="username">
<br/>
密 码:<input type="password" name="psw">
<br/>
<input type="submit" value="登录">
</form>
</div>
</body>
</html>
Filter过滤概念
过滤器实际上就是对web资源进行拦截,做一些处理后再交给下一个过滤器或servlet处理,通常都
是用来拦截request进行处理的,也可以对返回的response进行拦截处理,通常用来做一些通用配
置,比如:权限验证,登录验证,乱码处理等
单词:
Parameter:参数,变量