Burak Keceli
Burak Keceli

Reputation: 953

JSP - how to create a link from a jsp page to another jsp page

I am new at JSP. And I know this is the basic question. But I could not do it. What I want is to create a link in this jsp page. However the other page will be different based on login. If it is correct, a link to correct.jsp must be shown, if login is not correct, a link to login.jsp must be shown.

<%
    String str = "";
    String userid = request.getParameter("usr");
    session.putValue("userid", userid);
    String pwd = request.getParameter("pwd");
    Class.forName("com.mysql.jdbc.Driver");
    java.sql.Connection con = DriverManager.getConnection(
            "jdbc:mysql://localhost:3306/pr", "root", "xxx");
    Statement st = con.createStatement();
    ResultSet rs = st
            .executeQuery("select * from a where name='"+ userid + "'");
    if (rs.next()) {
        if (rs.getString(2).equals(pwd)) {
            out.println("welcome " + userid);
            str = "correct.jsp";
        } else {
            out.println("Invalid password try again");
            str = "login.jsp";
        }
    }
%>

<a href=str> <% str; &> </a>

However when I do this, the error of "insert "AssignmentOperator Expression" to complete Expression" for <% str; &> is given.

Thanks,

Upvotes: 4

Views: 59818

Answers (4)

Akash Shinde
Akash Shinde

Reputation: 955

<%
String str = "";
String userid = request.getParameter("usr");
session.putValue("userid", userid);
String pwd = request.getParameter("pwd");
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection con = DriverManager.getConnection(
        "jdbc:mysql://localhost:3306/pr", "root", "xxx");
Statement st = con.createStatement();
ResultSet rs = st
        .executeQuery("select * from a where name='"+ userid + "'");
if (rs.next()) {
    if (rs.getString(2).equals(pwd)) {
        out.println("welcome " + userid);
       // str = "correct.jsp";  
   %>
       <jsp:forward page="correct.jsp"></jsp:forward>
    <%
    } else {
        out.println("Invalid password try again");
       // str = "login.jsp"; 
   %>

       <jsp:forward page="login.jsp"></jsp:forward>
    <%
    }
}
%>

This seems to be standard method using jsp:forward tag.

Upvotes: 2

H.Rabiee
H.Rabiee

Reputation: 4837

In that case in order to output the string str you use the following expression <%=str%>. Hence <a href="<%=str%>">my link </a> should do it

Upvotes: 0

Rohan
Rohan

Reputation: 3078

You can use <a href="<%=str%>"><%=str%></a> I have edited it as you said.

Upvotes: 0

Eng.Fouad
Eng.Fouad

Reputation: 117589

Did you mean?

<a href="<%=str%>"> the_link </a>

Upvotes: 0

Related Questions