小弟初学这个,做一个小登陆程序,结果在jsp提交给servlet后就是没有任何跳转,是白的,数据库是mysql,user表中有user_id和user_password字段,下面是关键的代码:
web.xml,其中Checkpwd在bean包中:
<web-app>
<!-- JSPC servlet mappings start -->
<servlet>
<servlet-name>check</servlet-name>
<servlet-class>bean.Checkpwd</servlet-class>
</servlet>
<!-- JSPC servlet mappings end -->
<servlet-mapping>
<servlet-name>check</servlet-name>
<url-pattern>/checkin</url-pattern>
</servlet-mapping>
</web-app>
checkin.jsp是提交信息的页面,只使用了用户名和密码,提交给checkin:
<form id="form1" name="form1" method="post" action="checkin">
<table width="421" height="214" border="1" align="center">
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td width="133" height="38"><div align="center">用户名</div></td>
<td width="272"><input type="text" name="username" /></td>
</tr>
<tr>
<td height="41"><div align="center">密码</div></td>
<td><label>
<input type="text" name="password" />
</label></td>
</tr>
<tr>
<td height="38"><div align="center">验证码</div></td>
<td><input type="text" name="checkcode" /></td>
</tr>
<tr>
<td height="34"> </td>
<td><label>
<input type="submit" name="Submit" value="提交" />
<input type="reset" name="Submit2" value="取消" />
</label></td>
</tr>
</table>
</form>
Checkpwd.java是接受jsp中表单的servlet,里面就写了这一个函数:
public final class Checkpwd extends HttpServlet {
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
req.setCharacterEncoding("GBK");
String user=req.getParameter("username");
String pwd=req.getParameter("password");
String target=null;
ResultSet rs;
DB db=new DB();
Operate operate=new Operate();
try{
String sql="select * from user where user_id="+user+ "'";
rs=db.executeQuery(sql);
if(rs.next())
{
String temp=rs.getString("user_password");
if(temp.equals(pwd))
resp.sendRedirect("index.html");
}
else
resp.sendRedirect("checkin.jsp");
}
catch(Exception ee){
ee.printStackTrace();
}
}
}
DB.java是连接数据库的JavaBean,其中查询部分:
public ResultSet executeQuery(String sql) {
try {
connect = DriverManager.getConnection("jdbc:mysql://localhost/ehospital", "root", "xiangyang");
Statement stmt = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
}
catch(SQLException ex) {
ex.printStackTrace();
}
return rs;
}
jsp+servlet+javabean登陆问题
最新推荐文章于 2019-08-19 20:47:00 发布