2

I have one jsp file named test.jsp. I want to make link to a function which is in another jsp file named test2.jsp. My test.jsp has the following code:

<%@ page import="java.sql.*" %>
<% Class.forName("com.mysql.jdbc.Driver"); %>
<HTML>
<HEAD>
    <TITLE></TITLE>
</HEAD>

<BODY>
    <H1></H1>
<% 
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/xmldb", "root", "root");

        Statement statement = connection.createStatement() ;
        ResultSet resultset = statement.executeQuery("select DISTINCT title from categoryInfoTable") ; 
    %>
 <TABLE>
        <% while(resultset.next()){ %>
        <TR>
           <TD>
               <a href="test1.jsp?value1=<%=resultset.getString(1)%>"><%=resultset.getString(1)%></a>
           </TD> 
        </TR>
        <% } %>
    </TABLE>
</BODY>
</HTML>

And my test2.jsp code is as follows:

 <%@ page import="java.sql.*" %>
<% Class.forName("com.mysql.jdbc.Driver"); %>
<HTML>
<HEAD>
    <TITLE></TITLE>
</HEAD>
<BODY>
    <H1></H1>
<% 
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/xmldb", "root", "root");
        Statement statement = connection.createStatement() ;
        ResultSet results = statement.executeQuery("select Images from categoryConfigTable where parentCID = 20 && sequenceNum = 1;") ; 
    %>
<% 
        String req_received= request.getParameter("value1");
        if(req_received=="Images")
        {
        while(results.next()){ %>
<TABLE>
        <TR>
           <TD>
               <a href="<%=results.getString(1)%>.jsp"><%=results.getString(1)%></a>
           </TD> 
        </TR>      
    </TABLE>
        <% }} %> 
</BODY>
</HTML>

when i run test.jsp i get multiple links as it was supposed to be. And when i click on Images it goes to URL

http://localhost:8080/media/test1.jsp?value1=Images

But it doesn't show anything on this page. Why so? Is my code wrong anywhere. Please help me.

Well-Wisher
  • 41
  • 1
  • 9
  • you should `dbconnection class` separate from jsp – Bhargav Modi Feb 25 '15 at 05:29
  • I didn't get what you said. Can you show it in code – Well-Wisher Feb 25 '15 at 05:32
  • you should create a class in which you should keep your connection/business logic and than just import that class using `<%@ page import="your.package.YourClass*" %>` or if you want to stick with your code use this [code](http://stackoverflow.com/a/18823879/2749470) – Bhargav Modi Feb 25 '15 at 05:36
  • As you said i created separate `jsp file` for db connections and in my `test.jsp` i included: `<%@include file="dbconnection.jsp"%>` but still i am getting blank page when i click on link – Well-Wisher Feb 25 '15 at 05:53
  • Hey Thanks i got.. I changed my if condition and it worked – Well-Wisher Feb 25 '15 at 06:02
  • well if it helped you shall I post my answer if you are ready to accept it ?? – Bhargav Modi Feb 25 '15 at 06:53

1 Answers1

0

Change your if block in test2.jsp as

String req_received= request.getParameter("value1");
if(req_received != null && req_received.equals("Images"))
{
Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89