Cookie作用
Cookie:提供浏览器关闭后数据不会丢失的机制
Cookie:默认与域名绑定,只有域名下的程序才能读取数据
Cookie时效性
Cookie:具有时效性,Cookie会伴随请求头发送给服务器
Cookie:默认情况下Cookie有效期与当前浏览器进程绑定
Cookie:设置过期时间后,Cookie过期后才会失效
如何看网页存在的Cookie
查看网页旁边的小锁就可以看到有哪些cookie
如何自己向网页添加Cookie
设置cookie:
Cookie cookie=new Cookie("random",String.valueOf(random));//cookie名字与cookie值
添加cookie:
resp.addCookie(cookie);
设置最大生存时间:
cookie.setMaxAge(60*60*24);//设置最大生存时间
@WebServlet("/cookie/random1")
public class RandomServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Integer random=new Random().nextInt(10000);
Cookie cookie=new Cookie("random",String.valueOf(random));
cookie.setMaxAge(60*60*24);//设置最大生存时间
resp.addCookie(cookie);
resp.setContentType("text/html;charset=utf-8");
resp.getWriter().println("<h2>生成的随机数为:"+random+"</h2>");
}
}
如何自己向网页提取Cookie
提取操作:
Cookie[] cookies = req.getCookies(); for (Cookie c:cookies){ if (c.getName().equals("random")) random= Integer.parseInt(c.getValue()); }
验证操作(因为cookie被销毁后去获取cookie的话会报500错误)
if (cookies == null){ resp.getWriter().println("未包含任何数据"); return; }
@WebServlet("/cookie/show1")
public class CookieServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
Cookie[] cookies = req.getCookies();
Integer random=-1;
if (cookies == null){
resp.getWriter().println("未包含任何数据");
return;
}
for (Cookie c:cookies){
if (c.getName().equals("random"))
random= Integer.parseInt(c.getValue());
}
resp.getWriter().println("<h2>name=random的cookie值为:"+random+"</h2>");
}
}
Session作用
1.保密性更高,数据保存在服务器内存中,相当于钱存银行
2.默认时长为30分钟。Cookie默认时长为浏览器进程
3.Seesion通过浏览器Cookie的SeesionId值提取用户数据
4.session.setAttribute设置的属性生存周期与应用范围比自定义属性(request.setAttribute)更广
Session常用方法
创建session
@WebServlet("/session/random")
public class RandomServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Integer random=new Random().nextInt(10000);
HttpSession session = req.getSession();//创建session
session.setAttribute("random",random);//设置session值
resp.setContentType("text/html;charset=utf-8");
resp.getWriter().println("<h2>随机数"+random+"已生成</h2>");
}
}
获取session
@WebServlet("/session/show")
public class SessionServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
Integer random = (Integer)session.getAttribute("random");
resp.setContentType("text/html;charset=utf-8");
resp.getWriter().println("name=random的session值为:"+random);
}
}
ServletContext作用
一般用于网站恒定不变的备用信息
设置ServletContext
@WebServlet("/servletcontext/init")
public class ServletContextInitServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext servletContext=request.getServletContext();
servletContext.setAttribute("laodi","老弟");
response.setContentType("text/html;charset=utf-8");
response.getWriter().println("ServletContext已初始化");
}
}
提取ServletContext
@WebServlet("/servletcontext/index")
public class IndexServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
ServletContext context=request.getServletContext();
String laodi = (String)context.getAttribute("laodi");
PrintWriter out= response.getWriter();
out.println(laodi);
}
}
设置web应用默认首页-->在xml中加入以下代码设置
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>login.html</welcome-file>
</welcome-file-list>
设置404,500等状态码默认页面
<error-page>
<error-code>404</error-code>
<location>/error/404.html</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error/500.html</location>
</error-page>
设置启动时加载Servlet
代码一
<servlet>
<servlet-name>initTable</servlet-name>
<servlet-class>startup.InitTableServlet</servlet-class>
<!-- 自定义参数-->
<init-param>
<param-name>database</param-name>
<param-value>db01</param-value>
</init-param>
<!-- 设置启动时加载,且定义优先级-->
<load-on-startup>0</load-on-startup>
</servlet>
public class InitTableServlet extends HttpServlet {
@Override
public void init(ServletConfig config) throws ServletException {
String database = config.getInitParameter("database");
System.out.println("正在向"+database+"创建数据表...");
}
}
代码二
<servlet>
<servlet-name>import</servlet-name>
<servlet-class>startup.ImportServlet</servlet-class>
<init-param>
<param-name>file</param-name>
<param-value>c:/tmp/demo.txt</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
public class ImportServlet extends HttpServlet {
@Override
public void init(ServletConfig config) throws ServletException {
String file = config.getInitParameter("file");
System.out.println("正在导入"+file+"数据...");
}
}
JavaWeb的打包与发布
1.利用maven打包
2.利用maven清理包-->点击clean
3.maven打包时一般为jar包,需要在pom.xml文件中定义打包输入为war包
<packaging>war</packaging>
4.如果遇到打包失败时请在pom.xml文件中加入以下代码
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.0</version>
</plugin>
</plugins>
</build>
如何定义某些jar包文件不被重复打包
如何更改war包输出的名字
<build>
<finalName>web-setvlet</finalName>
</build>
如何打开包好的war包
1.打开文件夹
如何运行包好的war包
1.复制这个war包
2.来到webapps目录,粘贴在里面
3.来到bin目录下的starup打开-->此时刚刚的war包会被自动解压缩,双击打开进入即可
此时这个war包就已经在运行了
4.默认情况下进入此时包内的网页时需要如下加入包名-->但是此时就出现了一个问题,跳转到包内其他网页时,包名不会被输入
所以此时需要进入conf目录设置访问上下文路径
步骤:tomcat-->conf-->server.xml-->在</Host>上方加入以下代码即可