**
**
2019.08.12
安装IntelliJ IDEA
下载地址:http://www.jetbrains.com/idea/#chooseYourEdition
破解码地址:http://idea.lanyus.com/
在host文件中加入
0.0.0.0 account.jetbrains.com
0.0.0.0 www.jetbrains.com
下载Tomcat并部署
下载地址:http://tomcat.apache.org/download-80.cgi
bin文件下startup.bat启动服务
shutdown.bat关闭服务
bin文件夹(脚本文件)
conf文件夹(配置目录)
lib文件夹(依赖的jar包)
webapps文件夹(web项目目录)
work文件夹(tomcat处理jsp页面工作目录)
了解HTTP
超文本传输协议用于定义客户端浏览器和和服务器之间交换数据的过程
请求-相应模型
HTTP请求协议:请求行、请求头、请求体
HTTP响应协议:响应行、响应头、响应体
Servlet技术
功能:交互式的浏览和修改数据,生成动态Web内容,处理和解析HTTP请求参数
编写过程:
配置文件
类名
servlet类所在路径
类名
自定义路径
Servlet3.0 不需要写配置文件,只需要在@webServlet中添加url:
@WebServlet(name = "testServlet", urlPatterns = “自定义路径”)
!问题:
第一次继承HttpServlet时出现can’t resolve symbol servlet的错误
原因是没有导入servlet-api.jar
解决方法是快捷键F4进入Libraries,servlet-api.jar在tomcat的lib目录中
2019.08.13
HttpServletRequest
常用方法:
获取客户端请求参数方法:
HttpServletResponse
实战:切换验证码
html页面
切换验证码
VerifyCodeServlet代码
@WebServlet(name = "VerifyCodeServlet", urlPatterns = "/code")
public class VerifyCodeServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int width = 100;
int height = 30;
String data="abcdejksnfkgwglemrg12346789";
Random random=new Random();
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics graphics=image.getGraphics();
graphics.setColor(Color.gray);
graphics.fillRect(0,0,width,height);
graphics.setColor(Color.black);
for(int i=0; i<4; i++){
int position=random.nextInt(data.length());
String randomStr=data.substring(position,position+1);
graphics.drawString(randomStr,width/5*(i+1), 15);
}
ImageIO.write(image,"jpg",response.getOutputStream());
}
Servlet生命周期
(第一次访问Servlet)装载Servlet类并创建实例->调用init(Servlet config)初始化Servlet->调用service(ServletRequest, ServletResponse)->执行doGet和doPost->服务器关闭执行destroy()
ServletConfig对象获取Servlet相关配置信息
对应web.xml中
name
value
ServletContext对象表示当前的web应用环境信息
对应web.xml中
name
value
利用ServletContext可以实现多个Servlet共享参数,方法如下:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String data="xiaoming";
this.getServletContext().setAttribute("name",data);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name=(String) this.getServletContext().getAttribute("name");
System.out.println(name);
}
方法:
Servlet跳转
request.getRequestDispatcher("目标路径").forward(request,response);
response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
response.setHeader("Location","目标路径");
或者
response.sendRedirect("目标路径");
两者的区别:
请求转发浏览器地址不会改变,请求重定向会改变地址
请求转发只有一个request和一个response,请求重定向有两组
请求转发只需要内部资源名,请求重定向访问内部资源需要在URL中加入项目名称
实战:登录失败跳转页面并提示错误信息,登录成功跳转至百度界面
登录html界面如下
servlet代码
@WebServlet(name = "ServletTest2", urlPatterns = "/login")
public class ServletTest2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String userName="xiaoming";
String userPassword="123456";
String name= request.getParameter("name");
String password=request.getParameter("passWord");
if(!name.equals(userName)){
request.setAttribute("errorMessage","账户不存在");
request.getRequestDispatcher("/loginError.jsp").forward(request,response); //请求转发
}else if(!password.equals(userPassword)){
request.setAttribute("errorMessage","密码错误");
request.getRequestDispatcher("/loginError.jsp").forward(request,response);
}else{
response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY); //重定位
response.setHeader("Location","https://www.baidu.com/");
response.sendRedirect("/test_war_exploded/verifyCode.html");
}
}
}
错误界面loginError.jsp
用户登录错误界面
<%=request.getAttribute("errorMessage")%>
2019.08.14
实战:文件下载
web下建立download文件夹,存储下载资源。
DownloadServlet代码如下:
@WebServlet(name = "DownloadServlet",urlPatterns = "/download")
public class DownloadServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//通知浏览器下载文件,而不是浏览
String name = request.getParameter("filename");
String mimeType=this.getServletContext().getMimeType(name);
response.setContentType(mimeType);
response.setHeader("Content-Disposition","attachment;filename="+name);
//浏览文件
/*String realpath= this.getServletContext().getRealPath("download/"+name); //获取文件路径
InputStream in=new FileInputStream(realpath); //写入输入流
ServletOutputStream outputStream=response.getOutputStream();
int len=0;
byte[] buffer=new byte[1024];
while((len=in.read(buffer))>0){
outputStream.write(buffer,0,len); //输出流从缓冲区中读数据
}
in.close();*/
}
}
ps:测试时需要手动在浏览器URL中传参数filename
会话技术
从打开一个浏览器访问某个站点,到关闭这个浏览器的整个过程称为一次会话。会话技术就是记录这次会话中客户端的状态与数据的。
会话技术分为:
cookie的会话流程:
创建cookie
cookieServlet
Cookie cookie=new Cookie("goods","cup");
Cookie cookie1=new Cookie("userName","xiaoming");
cookie.setMaxAge(60*10); //设置cookie保存时间,消除cookie设这个时间为0
cookie.setPath("/test_war_exploded/getCookieServlet"); //设置访问getCookieServlet才携带cookie
response.addCookie(cookie);
response.addCookie(cookie1);
ps:使用Google浏览器开发者工具抓包查看cookie
获取cookie
getCookieServlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie[] cookies=request.getCookies();
for(Cookie cookie:cookies){
String name = cookie.getName();
if(name.equals("userName")){
String cookieValue=cookie.getValue();
response.getWriter().write("userName:"+cookieValue);
}
}
}
实战:记录网站上一次访问时间
TimeServlet
@WebServlet(name = "TimeServlet", urlPatterns = "/time")
public class TimeServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Date date=new Date();
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd-hh:mm:ss"); //时间格式化
String time = simpleDateFormat.format(date);
Cookie cookie=new Cookie("time",time);
cookie.setMaxAge(60*60*24);
response.addCookie(cookie);
response.setContentType("text/html;charset=UTF-8"); //解决乱码问题
Cookie[] cookies=request.getCookies();
String timevalue=null;
for(Cookie cookie1:cookies){
if(cookie1.getName().equals("time")){
timevalue=cookie1.getValue();
}
}
if(timevalue==null){
response.getWriter().write("欢迎您访问");
}else{
response.getWriter().write("您上次访问网站的时间是:"+timevalue);
}
}
}
Session的会话流程
Session为每个客户端创建一块内存空间存储客户的数据,客户端需要每次都携带一个标识ID去服务器中寻找自己的内存空间。是基于cookie的,Cookie存储客户的唯一标识JSESSIONID
2019.08.15
实例:简单实现模拟购物车功能
addCart.html
商品列表
杯子
书包
笔记本
getCartServlet
@WebServlet(name = "getCartServlet", urlPatterns = "/getCart")
public class getCartServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session=request.getSession();
List list=(List) session.getAttribute("list");
response.setContentType("text/html;charset=UTF-8");
for(String s:list){
response.getWriter().write(s+"
");
}
}
}
addCartServlet
@WebServlet(name = "addCartServlet",urlPatterns = "/addCart")
public class addCartServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name=request.getParameter("name");
HttpSession session=request.getSession();
List list=(List) session.getAttribute("list");
if(list==null){
list=new ArrayList();
}
list.add(name);
session.setAttribute("list",list);
Cookie cookie= new Cookie("JESESSION",session.getId());
cookie.setMaxAge(60*60*24);
cookie.setPath("/test_war_exploded");
response.addCookie(cookie);
}
}
2019.08.16
JSP简介
JSP用于显示数据和获取数据,JPS作为请求页面,显示表单或者超链接;作为请求结束页面,显示数据
而Sevlet是数据的中间处理环节
jsp=html+java脚本+jsp标签
3种java脚本
<%…%>:用于写java代码片段
<%=…%>:用于输出表达式
<%!..%>:用于声明一个成员变量
jsp注释
<%–…--%> 此种注释仅在jsp文件中显示
jsp九大隐式对象
四大域
jsp标签
EL表达式
${pageScope/ requestScope/ sessionScope/ applicationScope.参数名} 获取域中数据
EL内置对象
pageScope,requestScope,sessionScope,applicationScope 获取JSP域中对象
param,paramValues 接收参数
相当于request.getParameter() request.getParameterValues()
header,headerValues 获取请求头信息
相当于request.getHeader(name)
initParam 获取全局初始化参数
相当于this.getServletContext().getInitParameter(name)
cookie WEB开发中cookie
相当于request.getCookies()–>cookie.getName()–>cookie.getValue()
pageContext WEB开发中的pageContext
获得其他八大对象
JSTL
JSP标准标签库,含有五个子库
核心标签库(core)
下载地址:http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/
将lib中的jstl.jar 和standard.jar加入到项目中
settings中Schemas and DTDs中
URI中填写链接http://java.sun.com/jsp/jstl/core
file选择tld文件夹中的c.tld
最后在jsp页面中添加<%@taglib uri=“http://java.sun.com/jsp/jstl/core” prefix=“c”%>
2019.08.20
监听器Listener
按监听对象分类:
监听对象属性的增加、修改和删除:
各监听器调用时机:
ServletContextLisener:servlet容器启动和关闭
HttpSessionLisener:调用getSession()创建session对象时调用
ServletRequestLisener:每次发起请求时都会调用
过滤器Filter
JAVA WEB之过滤器