具体登录实现代码:
前端代码:
后台代码:
loginservlet:
package denglu.cookie;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/logincookie")
public class loginservlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
req.setCharacterEncoding("UTF-8");
String username=req.getParameter("username");
//System.out.println(name);
//String password=req.getParameter("userpad");
resp.setContentType("text/html;charset=UTF-8");
PrintWriter out=resp.getWriter();
System.out.println(URLEncoder.encode(username,"UTF-8"));
Cookie cook=new Cookie("username", URLEncoder.encode(username,"UTF-8"));
resp.addCookie(cook);
out.print("欢迎:'"+ username +"'使用");
out.print("您有邮件");
}
}
chaxunservlet:
package denglu.cookie;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/chaxuncookie")
public class chaxunservlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
req.setCharacterEncoding("UTF-8");
String username=null;
resp.setContentType("text/html;charset=UTF-8");
Cookie[] cookies=req.getCookies();
for (Cookie cook : cookies) {
if("username".equals(cook.getName()))
{
String name=cook.getValue();
username=URLDecoder.decode(name,"UTF-8");
System.out.println(username);
}
}
PrintWriter out=resp.getWriter();
out.print("欢迎:'"+ username +"'使用");
out.print("第一封邮件");
out.print("第二封邮件");
out.print("第三封邮件");
}
}
contentservlet:
package denglu.cookie;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/contentcookie")
public class contentservlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
req.setCharacterEncoding("UTF-8");
String username=null;
resp.setContentType("text/html;charset=UTF-8");
Cookie[] cookie=req.getCookies();
for (Cookie cook : cookie) {
if("username".equals(cook.getName()))
{
String value=cook.getValue();
username=URLDecoder.decode(value,"UTF-8");
System.out.println(username);
}
}
PrintWriter out=resp.getWriter();
out.print("欢迎:'"+ username +"'使用");
out.print("所谓的焦虑就是书读得太少,而想得又太多了");
}
}