所以我对JSP很新,但我一直在jsp中获得类,接口或枚举预期错误。
我是否必须将scriptlet嵌套在声明本身中?
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%!
PreparedStatement psmt;
Connection con;
public void jspInit(){
try{
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/JSPData?zeroDateTimeBehavior=convertToNull", "root","root");
psmt = con.prepareStatement("Insert into jspData Values(?,?,?)");
}
catch(Exception e){}
}
%>
<%!
String uname;
String pwd;
String email;%>
<%
uname= request.getParameter("uname");
pwd = request.getParameter("pwd");
email=request.getParameter("email");
psmt.setString(1,uname);
psmt.setString(2,pwd);
psmt.setString(3,email);
int i=stmt.executeUpdate();
if(i>0){
out.println("<h2>Record Inserted</h2>");
}
%>
<%!
public void jspDestroy() {
try {
con.close();
}
catch(Exception e){
e.printStackTrace();}
}
}
%>
</body>
</html>
更具体地说,我在我的代码中得到了这些错误:
pwd = request.getParameter("pwd");
email=request.getParameter("email");
psmt.setString(1,uname);
psmt.setString(2,pwd);
psmt.setString(3,email);
int i=stmt.executeUpdate();
if(i>0){
out.println("<h2>Record Inserted</h2>");
}
我特别不明白为什么我没有收到uname= request.getParameter("uname");
行的错误,但是我在下面的密码和电子邮件的类似初始化中得到了它。