简单的JSP页面没有加载

时间:2013-07-30 09:33:50

标签: java html jsp

获取以下jsp代码的错误:

 <%@ page import ="java.sql.*" %>
<%@ page import ="javax.sql.*" %>
<%
String user=request.getParameter("userid"); 
session.putValue("userid",user); 
String pwd=request.getParameter("pwd"); 
String fname=request.getParameter("fname"); 
String lname=request.getParameter("lname"); 
String email=request.getParameter("email"); 
Class.forName("com.mysql.jdbc.Driver"); 
java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test",
"root","123"); 
Statement st= con.createStatement(); 
ResultSet rs; 
int i=st.executeUpdate("insert into users values ('"+user+"','"+pwd+"','"+fname+"',
'"+lname+"','"+email+"')"); 


%>

我得到的错误如下:

org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 15 in the jsp file: /db/reg.jsp
String literal is not properly closed by a double-quote
12: "root","123"); 
13: Statement st= con.createStatement(); 
14: ResultSet rs; 
15: int i=st.executeUpdate("insert into users values ('"+user+"','"+pwd+"','"+fname+"',
16: '"+lname+"','"+email+"')"); 
17: 
18: 


Stacktrace:
    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:102)
    org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:331)
    org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:469)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:378)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:646)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

我不知道这里出了什么问题。我是jsp的新手,所以请帮我一个简单的解释。端口号,用户名密码以及其为jsp文件更正的所有内容。我还有别的东西可以忽略。请帮我纠正错误。

3 个答案:

答案 0 :(得分:1)

首先,don't use scriptlets in JSP file,您可以在Servlet或某些DAO类中使用此逻辑。

接下来,下面一行应该是一行:

java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123"); 

甚至把它放在一行:

int i=st.executeUpdate("insert into users values ('"+user+"','"+pwd+"','"+fname+"','"+lname+"','"+email+"')"); 

更好地使用占位符构造查询字符串并使用PreparedStatement并设置每个值。它将阻止所有这些字符串格式问题以及SQL Injection

String query = "insert into users values (?,?,?,?,?,?)";
PreparedStatement statement = connection.prepareStatement(query);
statement.setString(user);
.............
.............
int updateSuccessful = statement.executeUpdate();

答案 1 :(得分:0)

怀疑String格式,请尝试以下操作:

java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/"+
"test","root","123"); 

答案 2 :(得分:0)

您必须包含带双引号的字符串文字 - &gt; “。我无法运行代码,但你的错误在你的sql语句中。术语'插入用户值'被解释为一个字符串。