表单填写后的页面无法连接数据库
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
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>注册页面</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">
-->
<script language="javascript">
function mycheck(){
if (form1.UserName.value=="")
{alert("请输入用户名!");form1.UserName.focus();return;}
if(form1.PWD.value=="")
{alert("请输入密码!");form1.PWD.focus();return;}
form1.submit();
}
</script>
</head>
<body>
<form name="form1" method="POST" action="MyJsp.jsp"><!-- 提交的action根据struts-config.xml文件对应method后为FindUserAction的方法名 -->
<table width="364" height="145" border="0" align="center"
cellpadding="0" cellspacing="0">
<tr>
<td height="2" colspan="2"></td>
</tr>
<tr>
<td height="2" colspan="2" valign="top"></td>
</tr>
<tr>
<td width="54" height="22" valign="bottom">
<span class="STYLE15" >用户名:</span>
</td>
<td width="310" valign="bottom">
<input name="UserName" type="text" class="input2" onKeyDown="if(event.keyCode==13){form1.PWD.focus();}"
onMouseOver="this.style.background='#F0DAF3';"
onMouseOut="this.style.background='#FFFFFF'"><!-- 设置用户名文本框,并设置了鼠标经过是样式 -->
</td>
</tr>
<tr>
<td height="23" colspan="2" valign="bottom"></td>
</tr>
<tr>
<td height="34" colspan="2" valign="top" class="STYLE15">
密 码:
<input name="PWD" type="password" class="input2" align="bottom"
onKeyDown="if(event.keyCode==13){form1.yanzheng.focus();}"
onMouseOver="this.style.background='#F0DAF3';"
onMouseOut="this.style.background='#FFFFFF'"><!-- 设置密码文本框 -->
</td>
</tr>
<tr>
<td colspan="2" valign="top">
<input name="Submit" type="button" class="submit1" value="注册" onClick="mycheck()">
<input name="Submit2" type="reset" class="submit1" value="重置"> <!-- 设置提交与重置按钮-->
</td>
</tr>
</table>
</form>
</body>
</html>
这个是我的一个注册页面,我点击注册后,页面就会转到MyJsp.jsp里。MyJsp.jsp的代码
<%@ page language="java" pageEncoding="GB18030"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>JSP读取数据库</title>
</head>
<body>
<table border="1" align="center">
<tr>
<th>书号</th>
<th>书名</th>
<th>作者</th>
<th>出版社</th>
<th>单价</th>
<th>出版日期</th>
</tr>
<%
String driverClass = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/db_Database02";
String user = "root";
String password = "123";
Connection conn;
try {
Class.forName(driverClass).newInstance();
conn = DriverManager.getConnection(url, user, password);
Statement stmt = conn.createStatement();
String sql = "SELECT * FROM tb_books";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
%>
<tr>
<td><%=rs.getString("ISBN")%></td>
<td><%=rs.getString("bookName")%></td>
<td><%=rs.getString("publishing")%></td>
<td><%=rs.getString("writer")%></td>
<td><%=rs.getString("price")%></td>
<td><%=rs.getString("date")%></td>
</tr>
<%
}
} catch (Exception ex) {
ex.printStackTrace();
}
%>
</table>
</body>
</html>
为什么MyJsp.jsp里面看不到数据库里的东西,而我新建一个web project用MyJsp.jsp里面的代码建个index.jsp却能连接上数据库,查看数据?
[解决办法]
是不jsp 代码 放的位置不对呀、 WebContent下、 、
[解决办法]
<input name="Submit" type="button" class="submit1" value="注册" onClick="mycheck()">
你点注册,根本就不会跳转,只会触发一个mycheck()函数
[解决办法]
注册页面代码:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Insert title here</title></head><script type="text/javascript">function mycheck(){ if ( form1.UserName.value==""){ alert("请输入用户名!"); document.getElementById('username').focus(); return false; } if ( form1.PWD.value==""){ alert("请输入密码!"); form1.PWD.focus(); return false; } return true; }</script><body> <form name="form1" method="POST" action="MyJsp.jsp"><!-- 提交的action根据struts-config.xml文件对应method后为FindUserAction的方法名 --><table width="364" height="145" border="0" align="center"cellpadding="0" cellspacing="0"><tr><td height="2" colspan="2"></td></tr> <tr><td height="2" colspan="2" valign="top"></td></tr><tr><td width="54" height="22" valign="bottom"><span class="STYLE15" >用户名:</span></td><td width="310" valign="bottom"><input name="UserName" id="username" type="text" class="input2" onKeyDown="if(event.keyCode==13){form1.PWD.focus();}"onMouseOver="this.style.background='#F0DAF3';"onMouseOut="this.style.background='#FFFFFF'"><!-- 设置用户名文本框,并设置了鼠标经过是样式 --></td></tr><tr><td height="23" colspan="2" valign="bottom"></td></tr><tr><td height="34" colspan="2" valign="top" class="STYLE15">密 码:<input name="PWD" type="password" class="input2" align="bottom"onKeyDown="if(event.keyCode==13){form1.yanzheng.focus();}"onMouseOver="this.style.background='#F0DAF3';"onMouseOut="this.style.background='#FFFFFF'"><!-- 设置密码文本框 --></td></tr><tr><td colspan="2" valign="top"> <input name="submit" type="submit" class="submit1" value="注册" onclick="return mycheck()"> <input name="Submit2" type="reset" class="submit1" value="重置"> <!-- 设置提交与重置按钮--></td> </tr> </table> </form></body></html>
[解决办法]