简体   繁体   中英

How do I get the Url hits back in servlet from jsp? I want to count the number of times the url is clicked

I have a favoritelist.jsp to display the list of favorite urls,comments and clickcount.

     //favoritelist.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <%@page import="org.rss.beans.UserBean"%>
    <%@page import="org.rss.beans.FavoriteBean"%>
    <%@page import="java.util.ArrayList;"%>

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <jsp:useBean id="formHandler1" class="org.rss.beans.FavoriteBean"
        scope="request" />

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Welcome</title>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <style>

    table+table {
        margin-top: 20px
    }
    </style>
    </head>
    <body>
        <form action="processfavoritelist.jsp" method="post">

            <%
                UserBean currentUser = (UserBean)session.getAttribute("currentSessionUser");
            %>
            <%
                ArrayList<?> favoriteList =(ArrayList<?>) session.getAttribute("listFav");
            %>
            <center>

                <a>
                    Favorites for
                    <%=currentUser.getFirstName()  + " " + currentUser.getLastName()%>
                </a>
                <br>
                <font size=2><sup>*</sup> Required Fields</font>
                <table cellpadding=4 cellspacing=2 border=1>
                    <tr>
                        <td align="left">URL<sup>*</sup></td>
                        <td align="left"><input type="text" name="url"
                            value='<%=formHandler1.getUrl()%>' /> <br>
                        <font size=2 color=red><%=formHandler1.getErrorMsg("url")%></font>
                        </td>
                    </tr>
                    <tr>
                        <td align="left">Comment<sup>*</sup></td>
                        <td align="left"><input type="text" name="comment"
                            value='<%=formHandler1.getComment()%>' /> <br>
                        <font size=2 color=red><%=formHandler1.getErrorMsg("comment")%></font>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2" align="center"><input type="submit"
                            value="Add Favorite" name="addFavorite" /></td>
                    </tr>

                </table>

                <table cellpadding=4 cellspacing=2 border=3>
                    <tbody>
                        <tr>
                            <th>URL</th>
                            <th>Comments</th>
                            <th>Clicks</th>
                        </tr>
                        <c:forEach items="${listFav}" var="fav">
                            <tr>
                                <td><a href="<c:url value=''>
                                <c:param name="hits" value="${fav.clickCount}"/>
                                <c:param name="url" value="${fav.url}"/>
                                <c:param name="comment" value="${fav.comment}"/>

                                </c:url>">${fav.url}  </a>
                                </td>
                                <td><c:out value="${fav.comment}"></c:out></td>
                                <td><c:out value="${fav.clickCount}"></c:out></td>
                            </tr>
                        </c:forEach>
                    </tbody>
                </table>

            </center>
        </form>
    </body>
    </html>

The control will go to processfavoritelist.jsp where I check for any errors in the URL and Comments entered in the textfield. If it is valid, the control should go to the Login Servlet .

    //processfavoritelist.jsp

    <%@ page import="java.util.*" %>
    <%@ page import="java.lang.*" %>
    <jsp:useBean id="formHandler1" class="org.rss.beans.FavoriteBean" scope="request">
    <jsp:setProperty name="formHandler1" property="*"/>
    </jsp:useBean>

    <%System.out.println("----------------------Inside processfavoritelist.jsp");%>
    <% 
       if (formHandler1.validate("favoritelist")) {
    %>
         <jsp:forward page="login"/>
    <%
       }  else {
    %>
        <jsp:forward page="favoritelist.jsp"/>
    <%
       }
    %>

When I enter the URL and Comment in the textfield, the control goes back to servlet, inserts rec in db and displays back on the favoritelist.jsp. But when I click on any URL displayed in the second table, the url changes. //eg. myApp/favoritelist.jsp?hitid=21&url=asas&comment=sasasa
And there is no response after that. It does not even enter Login servlet where I have sysout statements to check. Thanks

    <td><a href="<c:url value=''>

YOU ARE NOT PROVIDING ANY LINK TO THE SERVLET OR JSP WHEN YOU CLICK ON IT. iT IS A DEAD LINK THATS WHY NO LOGIC IS RUNNING AND WHEN YOU CLICK ON THE LINK IT STAYS ON THE SAME PAGE.

Provide a link to jsp or servlet whatever processes your code and it will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM