使用JSP注释时编译错误?

时间:2014-04-19 15:07:43

标签: java jsp jdbc

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

}

%GT;

2 个答案:

答案 0 :(得分:1)

您无法在scriptlet <% ... %>中使用JSP注释。

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

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

答案 1 :(得分:0)

当你已经在一个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
    {

    }
%>