0

我的jsp中有以下代码

<table>
    <c:forEach var="link" items="${weblinks}">
        <c:if test="${link.featured}">
            <tr>
                <td>
                    <span>${link.title} (Hits : ${link.numOfHits})
                        </span>

                    <span>
                        <a href="<c:url value='${link.url}'/>">${link.url}  </a></span><br></td>
            </tr>
        </c:if>
    </c:forEach>
</table>

现在我希望当任何用户单击链接时链接打开并且链接的 url 也转到 servlet。我已经实现了第一个功能,但是我将如何在 servlet 中获取 url,以便我可以在数据库中更新点击次数,网站链接已收到?

请帮我。我有谷歌它,但没有得到答案。如果使用了javascript,那么请解释一下java脚本代码吗?

4

3 回答 3

1

更新

<a href="<c:url value='${link.url}'>
<c:param name="hits" value="${link.numOfHits}"/></c:url>">${link.url}  </a>

这将添加一个查询字符串,其参数为点击数,其值为点击数

在 servlet 上,request.getParameter("hits")您将获得 servlet 上的命中数

参考http://www.roseindia.net/jsp/simple-jsp-example/JSTLConstructingURLs.shtml

希望这可以帮助

于 2013-02-13T05:47:49.450 回答
0

我不知道您的链接是如何创建的,但看起来您将向您的 servlet 发出 GET 请求。知道这一点后,每个 servlet 都应该管理页面的计数器命中,并且由于每个用户都应该知道这个值,因此最好将它保存在应用程序范围内,而不是请求或会话中。更多内容和servlet 是如何工作的?实例化、会话、共享变量和多线程

我将发布一个处理单个链接计数器的 jsp 和 servlet 示例。您也应该能够使用它来处理您的链接。

index.jsp (其他元素喜欢<head>并且<html>对于示例毫无价值)

<body>
    Hit the button to add a value to the application counter
    <br />
    <form action="HitCounterServlet" method="GET">
        <input type="submit" value="Add counter hit" />
    </form>
    <br />
    Total hits: ${applicationScope['counter']}
</body>

HitCounterServlet

@WebServlet(name = "HitCounterServlet", urlPatterns = {"/HitCounterServlet"})
public class HitCounterServlet extends HttpServlet {

    private static final Object counterLock = new Object();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        ServletContext context = request.getServletContext();
        updateHitCounter(context);
        String originalURL = "index.jsp";
        //in case you want to use forwarding
        //request.getRequestDispatcher(originalURL).forward(request, response);
        //in case you want to use redirect
        response.sendRedirect(response.encodeRedirectURL(request.getContextPath() + "/" + originalURL));
    }

    private void updateHitCounter(ServletContext context) {
        //since more than a request can try to update the counter
        //you should protect the update using a synchronized block code
        synchronized(counterLock) {
            Integer counter = (Integer)context.getAttribute("counter");
            if (counter == null) {
                counter = 0;
            }
            counter++;
            context.setAttribute("counter", counter);
        }
    }
}

在不同的浏览器中尝试此操作,您将看到计数器如何在它们之间保持相同的状态。


为了将计数器命中保存在数据库中,您只需更改updateHitCounter函数中的代码以获取将连接到数据库的代码并对数据库字段执行更新语句。

于 2013-02-13T06:28:15.790 回答
-1

您可以使用 cookie 来记录页面点击次数。

代码:

<%@ page import="java.io.*,java.util.*" %>
<%
   // Get session creation time.
   Date createTime = new Date(session.getCreationTime());
   // Get last access time of this web page.
   Date lastAccessTime = new Date(session.getLastAccessedTime());

   String title = "Welcome Back to my website";
   Integer visitCount = new Integer(0);
   String visitCountKey = new String("visitCount");
   String userIDKey = new String("userID");
   String userID = new String("ABCD");

   // Check if this is new comer on your web page.
   if (session.isNew()){
      title = "Welcome Guest";
      session.setAttribute(userIDKey, userID);
      session.setAttribute(visitCountKey,  visitCount);
   } 
   visitCount = (Integer)session.getAttribute(visitCountKey;
   visitCount = visitCount + 1;
   userID = (String)session.getAttribute(userIDKey);
   session.setAttribute(visitCountKey,  visitCount);
%>
<html>
<head>
<title>Session Tracking</title>
</head>
<body>
<center>
<h1>Session Tracking</h1>
</center>
<table border="1" align="center"> 
<tr bgcolor="#949494">
   <th>Session info</th>
   <th>Value</th>
</tr> 
<tr>
   <td>id</td>
   <td><% out.print( session.getId()); %></td>
</tr> 
<tr>
   <td>Creation Time</td>
   <td><% out.print(createTime); %></td>
</tr> 
<tr>
   <td>Time of Last Access</td>
   <td><% out.print(lastAccessTime); %></td>
</tr> 
<tr>
   <td>User ID</td>
   <td><% out.print(userID); %></td>
</tr> 
<tr>
   <td>Number of visits</td>
   <td><% out.print(visitCount); %></td>
</tr> 
</table> 
</body>
</html>

或者您可以使用应用程序隐式对象和相关方法 getAttribute() 和 setAttribute() 实现命中计数器。

<%
    Integer hitsCount = 
      (Integer)application.getAttribute("hitCounter");
    if( hitsCount ==null || hitsCount == 0 ){
       /* First visit */
       out.println("Welcome to my website!");
       hitsCount = 1;
    }else{
       /* return visit */
       out.println("Welcome back to my website!");
       hitsCount += 1;
    }
    application.setAttribute("hitCounter", hitsCount);
%>
<center>
<p>Total number of visits: <%= hitsCount%></p>

希望这可以帮助..

于 2013-02-13T03:41:40.840 回答