📄 giftservlet.java
字号:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
public class GiftServlet extends HttpServlet
{
public void service(HttpServletRequest req, HttpServletResponse res) throws IOException
{
boolean cookieFound=false;
Cookie myCookie=null;
Cookie[] cookieset=req.getCookies();
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
pw.println("<HTML>");
pw.println("<BODY>");
for (int i=0;i<cookieset.length;i++)
{
if (cookieset[i].getName().equals("logincount"))
{
cookieFound=true;
myCookie=cookieset[i];
}
}
if (cookieFound)
{
int temp=Integer.parseInt(myCookie.getValue());
temp++;
if (temp==5)
{
pw.println("Congratulations!!!!!!!!!!!!!!!!!!!, a gift is awaiting you");
temp=0;
myCookie.setValue(String.valueOf(temp));
cookieFound=false;
}
else
{
pw.println("The number of times you have logged in is: "+String.valueOf(temp));
myCookie.setValue(String.valueOf(temp));
}
int age=60*60*24*30;
myCookie.setMaxAge(age);
res.addCookie(myCookie);
}
else
{
int temp=1;
pw.println("This is the first time you have logged on to the server");
myCookie=new Cookie("logincount",String.valueOf(temp));
int age=60*60*24*30;
myCookie.setMaxAge(age);
res.addCookie(myCookie);
}
pw.println("</BODY>");
pw.println("</HTML>");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -