用JSP写一个网络点击率记录器

用JSP写一个网站点击率的记录器
首先,创建一个登录页面login.html
代码如下:

 <html>
  <head>
   <meta charset="UTF-8">
       <title>网站点击率&middot点击率</title>
        </head>
         <body> 
         <center> 
        <h2>点击率</h2>   
         </center>
          <hr>
          <br>
           <center>
            <form method=post action="/city/gift"> 
               <table> <tr> <td>用户名:</td>
                <td><input type="text"    name="loginid"/></td>
                 </tr> <tr> <td>密&nbsp;&nbsp;码:</td>
                  <td><input    type="password" name="passwd"/></td>
                   </tr>
                    </table>
                   <br>
                     <input    type="submit" value="提交"> </form> 
                    </center> 
                    </body>
                     </html>

在这里插入图片描述

接着,创建一个GiftServlet.Java
代码如下:

package cn.edu.pyp.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
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;

/**
 * Servlet implementation class GiftServlet
 */
@WebServlet("/gift")
public class GiftServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {

		// TODO Auto-generated method stub
	}

	/**
	 * @see Servlet#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		boolean cookieFound = false;
		Cookie myCookie = null;
		Cookie cookies[] = request.getCookies();
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.println("<html>");
		out.println("<head><title>网站点击率 &middot 点击率</title></head>");
		out.println("<body bgcolor= \"orange\">");
		out.println("<center><h2>点击率</h2></center>");
		out.println("<hr><center>");
		if (cookies != null) {
			for (int i = 0; i < cookies.length; i++) {
				if (cookies[i].getName().equals("logincount")) {
					cookieFound = true;
					myCookie = cookies[i];
				}
			}
		}
		if (cookieFound) {
			int temp = Integer.parseInt(myCookie.getValue());
			temp++;
			if (temp == 5)
				out.println("恭喜您!!!!,一个礼物将寄给您,请耐心等待。");
			out.println("您访问本网店的次数是:" + String.valueOf(temp) + "次");
			myCookie.setValue(String.valueOf(temp));
			int age = 60 * 60 * 24 * 30;
			myCookie.setMaxAge(age);
			response.addCookie(myCookie);
			cookieFound = false;
			}else {
				int temp = 1;
				out.println("这是你第一此访问本店");
				myCookie = new Cookie("logincount",String.valueOf(temp));
				int age = 60*60*24;
				myCookie.setMaxAge(age);
				response.addCookie(myCookie);
				
			}
		
		out.println("</center></body>");
		out.println("</html>");

	}

}

在这里插入图片描述
这样简单的网页点击率记录器就出来啦,

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页