目录

在servlet中专门提供了相关的api,来操作Cookie和Session。
| 方法 | 说明 |
| HttpSession getSession(false/true) | 在服务器中获取会话,注意返回值 |
| Cookie[ ] getCookies() | 返回一个包含客户端发送请求的所有Cookie对象的数组,注意类型 |
| 方法 | 说明 |
| void addCookie(Cookie cookie) | 把指定的cookie添加到响应中 |
| 方法 | 说明 |
| Object getAttribute() | 返回指定名称的对象,如果没有指定名称的对象则返回null |
| void setAttribute() | 使用指定的名称绑定一个对象到该会话中 |
| boolean isNew() | 判断当前对象是不是新创建出来的会话 |
| 方法 | 说明 |
| String getName() | 返回cookie的名称 |
| String getValue() | 获取于cookie关联的值 |
| void setValue(String newValue) | 设置于cookie关联的值 |



处理登录请求的servlet:login
- package login;
- 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 javax.servlet.http.HttpSession;
- import java.io.IOException;
-
- //处理登录请求
- @WebServlet("/login")
- public class LoginServlet extends HttpServlet {
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- req.setCharacterEncoding("utf8");
-
- //1.获取用户名和密码
- String username = req.getParameter("username");
- String password = req.getParameter("password");
-
- //2.验证密码是否正确
- //这里如果写成是user.equals("王大锤"),如果user是null,就会触发空指针异常
- //equals()在方法内部对参数为null做了特别处理,因此可以写成user.equals("王大锤")
- if("王大锤".equals(username) && "666".equals(password)){
- //登陆成功
- // a) 创建一个会话,用户刚登陆成功之前没有回话
- //getSession
- // 、创建session和一个Httpsession对象
- // 、把这俩个内容以键值对的形式插入到哈希表里
- // 、再把sessionId通过set-cookie发送给客户端
- HttpSession session = req.getSession(true);
-
- //随意设置键值对
- session.setAttribute("username","王大锤");
-
- // b) 让响应重定向到主页
- resp.sendRedirect("index");
-
- }else{
- //登陆失败
- resp.setStatus(403);
- resp.setContentType("text/html; charset=utf8");
- resp.getWriter().write("登陆失败!用户名或密码输入错误!!!");
- }
- }
- }
生成登陆成功跳转主页的servlet:index
- package login;
- 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 javax.servlet.http.HttpSession;
- import java.io.IOException;
-
-
- //登陆成功之后跳转的页面
- @WebServlet("/index")
- public class IndexServlet extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //首页中先获取了session,此处的session是刚才登录成功创建出来的
- //这里的参数是false,表示不新建,如果不存在返回null就行
- HttpSession session = req.getSession(false);
- if(session == null){
- resp.setStatus(403);
- resp.setContentType("text/html; charset=utf8");
- resp.getWriter().write("您尚未登录,不能访问主页!");
- return;
- }
- String username = (String) session.getAttribute("username");
- resp.setContentType("text/html;charset=utf8");
- resp.getWriter().write("欢迎来到主页," + username + "大哥请喝茶~");
- }
- }
| 方法 | 说明 |
| Part getPart(String name) | 获取请求中给定name的文件,和input标签的name属性一样(和getParameter类似) |
| Collection | 获取所有的文件 |
| 方法 | 说明 |
| String getSubmittedFileName() | 获取提交的真实文件名 |
| String getContentType() | 获取提交文件的类型 |
| long getSize() | 获取文件的大小 |
| void write(String path) | 保存到指定路径 |
上传一个图片:
- package upload;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.MultipartConfig;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.Part;
- import java.io.IOException;
-
- @WebServlet("/upload")
- @MultipartConfig
- public class UploadServlet extends HttpServlet {
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- Part part = req.getPart("myfile");
- System.out.println(part.getSubmittedFileName());
- System.out.println(part.getSize());
- System.out.println(part.getContentType());
- part.write("d:/idea/dada/result.jpg");
- }
- }
如果对您有帮助的话,
不要忘记点赞+关注哦,蟹蟹
如果对您有帮助的话,
不要忘记点赞+关注哦,蟹蟹
如果对您有帮助的话,
不要忘记点赞+关注哦,蟹蟹