Ammar
Ammar

Reputation: 11

how to redirect the links to a particular webpage using jsp?

There is my jsp page in which a href links are there. So i have to redirect it to that particular web page when i click to that link.

The links are fetched from my database.

Below is my jsp page

    <%@ page import="java.sql.*" %>
<%ResultSet rs=null; %>
<html>
 <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>

Your Searched Results Are :  
</title>
     <link href="Desktop/style.css"  rel="stylesheet"  type="text/css" />
        </head>
<body bgcolor="8B4513">
<%
        Connection conn=null;
    try
    {
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    conn=DriverManager.getConnection

("jdbc:mysql://localhost:3306/tendermysql","root","root");
    Statement stmt=conn.createStatement();
rs=stmt.executeQuery("select * from Record");

 %>

<form>
<center>
<h1> Welcome to Ezest Tender Optimzed Search</h1>



<%
 while(rs.next())
    {
        %>

<a href= "<%rs.getString(2); %>" 
  onclick = "window.location.href=this.form.rs.getString(2)
    [this.form.rs.getString(2)]" > <% out.println(rs.getString(2)); %>                 
  </a>
    <br />


       <% } %>

<% }
    catch(Exception e)
    {
        out.println("Wrong Input" +e);
    }


%>
<br>
</center>
</form>
</body>
</html>

I tried it but the page is not redirecting...

Upvotes: 0

Views: 472

Answers (1)

user2575725
user2575725

Reputation:

Porblem here is that your ResultSet is forward only so you cannot use this line rs.getString(2) to get same record more than once inside loop. Use a temp variable to repeat the value. Also there is no need to give onclick attribute when href is given the same link.

String str;
while(rs.next()){
  str = rs.getString(2);%>
  <a href="<%=str%>"><%=str%></a><%
}

Sample code with href and onclick:

<a href='http://stackoverflow.com/' onclick='return confirm("Visit page?")'>Stackoverflow</a>

P.S. Make sure to close db connection

Upvotes: 1

Related Questions