我有点麻烦试图让这个工作。这个想法是,每次刷新页面时都会向上计数。我认为它与不可变对象有关,但我想用一个来获得不可变/可变对象的经验。
问题在于它只计算一次,然后它会停止向上计数......我似乎无法弄清楚究竟是什么导致它失败......
(我正在使用NetBeans来开发它,因此有任何奇怪的评论)
来源:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Question1;
import java.io.IOException;
import java.io.PrintWriter;
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;
/**
*
* @author Owner
*/
@WebServlet(name = "EX02W03", urlPatterns = {"/EX02W03"})
public class Q1Servlet extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
HttpSession session = request.getSession();
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet EX02W03</title>");
out.println("</head>");
out.println("<body>");
//create immutable object
Integer add = 1;
//create mutable object
Counter count = new Counter(add);
//check to see if the session exists (if it is the users first time)
if (session.getAttribute("timesLoggedIn") == null) {
count.increment();
out.println("<h1>This is your first visit!</h1>");
session.setAttribute("timesLoggedIn", count.getCounter());
}else{
count.increment();
out.println("<h1>You have visited this page " + count.getCounter() + " times! </h1>");
session.setAttribute("timesLoggedIn", count.getCounter());
}
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
柜台类:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Question1;
/**
*
* @author Owner
*/
class Counter {
private int count;
public Counter(int counts) {
count = counts;
}
public int getCounter(){
return count;
}
public void increment(){
count++;
}
}
**已修复 - 我修复了这个问题,放置了两个不可变/可变对象,它们将在“受保护的void processrequest ...”之前发布。
答案 0 :(得分:2)
创建一个过滤器(这样就不会复制并粘贴到每个servlet上),然后使用静态(http://www.caveofprogramming.com/frontpage/articles/java/java-for-beginners-static-variables-what-are-they/)int。
public class urlFilter implements Filter {
@Override
public void init(FilterConfig config) throws ServletException {
}
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws ServletException, IOException {
myStaticVariable ++;
}
@Override
public void destroy() {
}
}
然后在web.xml上
<filter>
<filter-name>urlRewriteFilter</filter-name>
<filter-class>com.example.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>urlFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
答案 1 :(得分:0)
在重新加载/刷新页面时创建新的Counter对象,因此它不会增加。下面的方法只是实现你正在尝试的另一种方法。
if (session.getAttribute("timesLoggedIn") == null) {
out.println("<h1>This is your first visit!</h1>");
session.setAttribute("timesLoggedIn", 1);
}else{
int i = Integer.parseInt(session.getAttribute("timesLoggedIn"));
out.println("<h1>You have visited this page " + i + " times! </h1>");
session.setAttribute("timesLoggedIn", i+1);
}