为什么mysql查询字符串查询=" select * from" + table +"&#34 ;;`表示名称不是来自另一个jsp页面。
为什么会议。 setAttribute(""," table");`无法将字符串从jsp导入jsp?
我的代码中有两个jsp文件,一个是用于选择用户名和密码的表,用于登录,文件名是statement.jsp,其他文件语句用于显示在statement3.jsp中选择的表中的数据。我想在另一个jsp页面中使用表字符串来显示该表。
错误
String query ="select * from " + table + ""; //for selection of table
session.setAttribute("", "table"); //To pass String table from statement.jsp to statement3.jsp to show table.
<%
ResultSet rs = null;
session.setAttribute("", "table");
String query ="select * from " + table + "";
rs=st.executeQuery(query);
%>
statement.jsp
<%
ResultSet rs = null;
String name=request.getParameter("username");
String abc=request.getParameter("password");
String gender = request.getParameter("gender");
if (gender != null) {
String table = gender.equals("teacher") ? "teacher2" : "student";
out.println(table);
String query ="select * from " + table + " where username='"+name+"' AND password='"+abc+"'";
rs=st.executeQuery(query);
out.println(rs);
if(rs.next())
{
response.sendRedirect("statement3.jsp");
}
else
{
response.sendRedirect("index.jsp");
}}
%>
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:93)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:435)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:298)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:277)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:265)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:564)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:302)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
答案 0 :(得分:2)
如果您想将变量table
的值放在会话中,您可以这样做:
String table = "whateverTableNameYouWant";
session.setAttribute("mySessionVariableName", table);
如果你想在另一个jsp中读取该值,你可以这样做:
String table = (String)session.getAttribute("mySessionVariableName");