使用JSP注释时编译错误



我在网页中使用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
{
}

%>

您不能在scriptlets <% ... %>中使用JSP注释。

JSP被编译成Java servlet类。scriptlet的内容直接写入servlet类。由于<%-- ... --%>不是有效的Java代码,因此会出现编译器错误。

在scriptlet中,使用常见的Java注释// .../* ... */

当您已经在scriptlet中时,不需要使用JSP方式进行注释。在scriptlet中,所有的代码都应该准确地显示在任何java源文件中是如何完成的。

<%
    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
    {
    }
%> 

最新更新