2013-02-13 31 views
0

我有下面的代码在我的jsp如何在用户点击链接时从jsp获取url值到servlet?

<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> 

现在我想的是,当链路上的任何用户点击链接打开,链接的网址也进入到servlet。我已经实现了第一个功能,但我怎样才能在servlet中获得url,这样我就可以在数据库中更新点击次数,网站链接已经收到了?

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

回答

-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> 

希望这有助于..

+0

我该如何使用它?你能给我任何示例代码吗? – 2013-02-13 03:43:07

+0

这是一个不好的例子。OP正在学习正确的方式,并且您正在引导他/她使用脚本。请阅读[如何避免JSP文件中的Java代码?](http://stackoverflow.com/a/3180202/1065197) – 2013-02-13 06:13:18

+0

好吧4我指出,我将审查代码并使用OP的最佳解决方案进行更新正在寻找.. – Lucky 2013-02-13 13:10:17

1

更新

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

这将增加其拥有的点击参数号具有命中

在数量值的查询字符串servlet with request.getParameter("hits")您将获得servlet上的点击次数

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

希望这有助于

+0

计数器命中必须保存在应用程序范围,而不是在请求范围。 – 2013-02-13 06:05:55

+0

这是使用必须管理的servlet的一部分。 – Meherzad 2013-02-13 06:37:25

+0

你甚至都没有在你的答案中提到它,你如何期望OP能够实现它? – 2013-02-13 06:38:47

0

我不知道如何创建你的链接,但它看起来像你会做一个GET请求,你的servlet。知道这一点,每个servlet都应该管理页面的计数器命中,并且由于每个用户都应该知道这个值,所以最好将它保存在应用程序范围中,而不是请求或会话中。更多这hereHow do servlets work? Instantiation, sessions, shared variables and multithreading

我将发布一个处理单个链接计数器的jsp和servlet样本。你应该可以使用它来处理你的链接。在不同的浏览器

的index.jsp(其它元素,如<head><html>是例子wortheless)

<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功能的代码,代码将连接到数据库,并执行UPDATE语句到你的数据库字段。

+0

但我面临的主要问题是,我的代码中没有表单提交。而我怎么能得到的价值从jsp的URL到servlet没有提交表单?我需要当用户点击链接时,链接将在用户窗口中打开,同时url(已点击)的值将被传递给servlet,我将自己更新链接的点击次数。你有什么想法我怎么能得到这个? @Luiggi Mendoza – 2013-02-13 09:44:30

+0

@AsmaIhsan你能提供一个你的URL的例子,你是如何设法打开链接并同时进入servlet的?请注意,这个*表单提交的例子类似于在URL中访问'http:// localhost//HitCounterServlet',因为它是一个GET请求。 – 2013-02-13 19:49:22

相关问题