撸一撸哦······

类代码:(创建项目,再建立包,然后建立类,最后敲代码,并配置web.xml信息-----部署并启动服务器)

package com.pzhu.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class SessionServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//设置请求编码格式
req.setCharacterEncoding("utf-8");
//设置响应编码格式
resp.setContentType("text/html;charset=utf-8");
//获取请求信息
String name="张三";
//处理请求信息
//创建session对象
HttpSession hs=req.getSession();
//设置session的存储时间
hs.setMaxInactiveInterval(5);
System.out.println(hs.getId());
//存储数据
hs.setAttribute("name",name);
//响应处理结果
//直接响应
resp.getWriter().write("session学习");
HttpSession hs1=req.getSession();
System.out.println("SessionServlet2.service()"+hs1.getId());
System.out.println("SessionServlet2.service()"+hs1.getAttribute("name"));

}
}

web.xml配置:

<?xml  version="1.0"  encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID"
version="2.5">
<servlet>
<servlet-name>SessionServlet</servlet-name>
<servlet-class>com.pzhu.servlet.SessionServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SessionServlet</servlet-name>
<url-pattern>/ss</url-pattern>
</servlet-mapping>
</web-app>

结果:

前端学习--013--Session技术的实战学习_xml


学习,学习,真的很累啊······主人·······小妲己·················