Java JSP - 如何创建从一个 jsp 页面到另一个 jsp 页面的链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16522972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSP - how to create a link from a jsp page to another jsp page
提问by Burak Keceli
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.
我是 JSP 的新手。我知道这是基本问题。但我做不到。我想要的是在这个jsp页面中创建一个链接。但是,其他页面将根据登录情况而有所不同。如果正确,则必须显示一个到correct.jsp 的链接,如果登录不正确,则必须显示一个到login.jsp 的链接。
<%
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.
但是,当我这样做时,“插入“AssignmentOperator 表达式”以完成表达式的错误 <% str; &> 给出。
Thanks,
谢谢,
采纳答案by Akash Shinde
<%
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.
这似乎是使用 jsp:forward 标记的标准方法。
回答by Eng.Fouad
Did you mean?
你的意思是?
<a href="<%=str%>"> the_link </a>
回答by Rohan
You can use <a href="<%=str%>"><%=str%></a>
I have edited it as you said.
你可以使用<a href="<%=str%>"><%=str%></a>
我已经按照你说的编辑了它。
回答by H.Rabiee
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
在这种情况下,为了输出字符串 str,您可以使用以下表达式 <%=str%>。因此<a href="<%=str%>">my link </a>
应该这样做