在写findPwd(找回密码)时会出现这样子的错误: Servlet.service() for servlet [jsp] in context with path [/web04] threw exception [Unable to compile class for JSP:;
翻译一下:[/web04]抛出异常[无法为jsp编译类:
但是一直不知道为什么错了,后来仔细看了看,终于发现了自己的错误。
原代码
String s1;//注意要赋值,不赋值会有错
String s2;
while(rs.next()){
s1=rs.getString(1);
s2=rs.getString(2);
}
%>
<center>
<font face="楷体" size="6" color="#000">找回密码界面</font>
<table width="300" height = "180" border="5" bordercolor="#A0A0A0">
<tr>
<th>用户名:</th>
<td><input type="text" name="username" value="<%= s1 %>"></td>
</tr>
<tr>
<th>密 码:</th>
<td><input type="text" name="password" value="<%= s2 %>"></td>
</tr>
</table>
</center>
修改后代码1
String s1=null;//注意要赋值,不赋值会有错
String s2=null;
while(rs.next()){
s1=rs.getString(1);
s2=rs.getString(2);
}
%>
<center>
<font face="楷体" size="6" color="#000">找回密码界面</font>
<table width="300" height = "180" border="5" bordercolor="#A0A0A0">
<tr>
<th>用户名:</th>
<td><input type="text" name="username" value="<%= s1 %>"></td>
</tr>
<tr>
<th>密 码:</th>
<td><input type="text" name="password" value="<%= s2 %>"></td>
</tr>
</table>
</center>
</body>
原因是没有给s1和s2赋初值。
全部代码:
<%@ page language="java" import="java.util.*,java.sql.*,java.net.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>taobao findPwd page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
//接收用户名和密码
String user = new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");
//连接数据库
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/db_01?serverTimezone=GMT%2B8";
Connection conn = DriverManager.getConnection(url,"root","guanhong520");//这步错可能是因为数据库属性安全中的名、密码不对或SQL的IP端口不是‘3306’
PreparedStatement pStmt = conn.prepareStatement("select * from user where uername = '" + user);
String sql = "select * from user where uername ='";
sql += user + "'";
ResultSet rs = pStmt.executeQuery(sql);
String s1=null;//注意要赋值,不赋值会有错
String s2=null;
while(rs.next()){
s1=rs.getString(1);
s2=rs.getString(2);
}
%>
<center>
<font face="楷体" size="6" color="#000">找回密码界面</font>
<table width="300" height = "180" border="5" bordercolor="#A0A0A0">
<tr>
<th>用户名:</th>
<td><input type="text" name="username" value="<%= s1 %>"></td>
</tr>
<tr>
<th>密 码:</th>
<td><input type="text" name="password" value="<%= s2 %>"></td>
</tr>
</table>
</center>
</body>
</html>
但是还是不知道为什么不赋初值,会导致无法为jsp编译类,有大佬知道吗
欢迎大家在下面留言呀