我在我的网页中使用 JSP,但以下代码返回以下编译错误。
An error occurred at line: 27 in the jsp file: /login.jsp
Syntax error on tokens, delete these tokens
24: con = DriverManager.getConnection("jdbc:odbc:base","root","root");
25:
26: String username= request.getParameter("uname");
27: String password= request.getParameter("pass"); <%-- Getting the password entered by the user --%>
28: String query = "SELECT * FROM users where uname=? AND pass=?";
29: stmt=con.prepareStatement(query);
30: stmt.setString(1,username);
但是删除第 27 行的注释后代码就可以正常工作了。
整个代码是:
<%
try{
Connection con=null;
PreparedStatement stmt = null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:base","root","root");
String username= request.getParameter("uname");
String password= request.getParameter("pass"); //Getting the password entered by the user
String query = "SELECT * FROM users where uname=? AND pass=?";
stmt=con.prepareStatement(query);
stmt.setString(1,username);
stmt.setString(2,password);
ResultSet rs = stmt.executeQuery();
session.setAttribute("name",username);
if(rs.next())
{
response.sendRedirect("Sitelogin.jsp");
}
}
catch(Exception e)
{
out.println(e);
}
finally
{
}
%>
最佳答案
您不能在 scriptlet 中使用 JSP 注释 <% ... %>
.
JSP 被编译成 Java servlet 类。 scriptlet 的内容直接写入 servlet 类中。如<%-- ... --%>
不是有效的 Java 代码,您会收到编译器错误。
在 scriptlet 内,使用常用的 Java 注释 // ...
和/* ... */
.
关于java - 使用JSP注释时编译错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23171675/