请问JSP链接MySQL数据库的问题

艾勒伯格 2015-04-07 08:49:05
login.html
<!DOCTYPE html>
<html>
<head>
<title>login.html</title>

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">

<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

</head>

<body>
<FORM action="login.jsp" method=post>
用户名:<input type="text" name="userName"> <br>
密码:<input type="password" name="password"> <br>
<input type="submit" name="submit" value="登录"/>
</FORM>
</body>
</html>

login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.sql.* "%>
<%
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>My JSP 'login.jsp' starting 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 userName = request.getParameter("userName");
String psw = request.getParameter("password");


try {
// 加载驱动程序
Class.forName("com.mysql.jdbc.Driver");
// URL指向要访问的数据库名test
String url = "jdbc:mysql://127.0.0.1:3307/test";
// MySQL配置时的用户名
String user = "root";
// MySQL配置时的密码
String password = "usbw";
// 连续数据库
Connection connection = DriverManager.getConnection(url, user, password);
if(!connection.isClosed())
System.out.println("Succeeded connecting to the Database!");
// statement用来执行SQL语句
Statement statement = connection.createStatement();
// 要执行的SQL语句
String sql = "select * from user where name='"+userName+"' and password='"+psw+"'";
// 执行SQL语句并返回结果集
ResultSet rs = statement.executeQuery(sql);

if (rs.next()) //用户名和密码正确,显示main.jsp
{
response.sendRedirect("main.jsp");
}
else //用户名和密码不匹配 重新输入
{
response.sendRedirect("login.jsp");
}
// 关闭结果集
rs.close();
// 关闭连接
connection.close();
} catch(ClassNotFoundException e) {
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch(SQLException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
%>
</body>
</html>

main.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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>My JSP 'main.jsp' starting 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>
This is main page. <br>
</body>
</html>

使用的是USB WebServer
请问就是输入了用户名和密码之后没有任何跳转
...全文
92 3 打赏 收藏 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
艾勒伯格 2015-04-07
  • 打赏
  • 举报
回复
艾勒伯格 2015-04-07
  • 打赏
  • 举报
回复


跳转后为空页面=。=
艾勒伯格 2015-04-07
  • 打赏
  • 举报
回复

87,984

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧