【发布时间】:2017-08-14 04:02:12
【问题描述】:
这将需要输入用户名和密码 将其传递给validation.jsp
login.jsp
<center>
<table border="1" cellpadding="5" cellspacing="2">
<thead>
<tr>
<th colspan="2">Login Here</th>
</tr>
</thead>
<tbody>
<tr>
<td>Username</td>
<td><input type="text" name="username" required/></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password" required/></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Login" />
<input type="reset" value="Reset" />
</td>
</tr>
</tbody>
</table>
</center>
</form>
</body>
</html>
validation.jsp
它将验证数据库中的数据并返回;
<%@ page import ="java.sql.*" %>
<%
try{
String username = request.getParameter("username");
String password = request.getParameter("password");
Class.forName("com.mysql.jdbc.Driver"); // MySQL database connection
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/tejas?" + "user=root&password=tejas");
PreparedStatement pst = conn.prepareStatement("Select user,pass from login where user=tejas and pass=te65");
pst.setString(1, username);
pst.setString(2, password);
ResultSet rs = pst.executeQuery();
if(rs.next())
out.println("Valid login credentials");
else
out.println("Invalid login credentials");
}
catch(Exception e){
out.println("Something went wrong !! Please try again");
}
%>`
这都是保存在 webapps 文件夹下的代码 webapps 文件夹包含 web-inf login.jsp validation.jsp
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1"
metadata-complete="true">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
</web-app>
我需要添加一些东西到 web.xml 请帮忙...
【问题讨论】:
-
jsp 用于显示目的,而不是数据库访问和业务逻辑。将此代码移动到 servlet 中
-
您的提交按钮不会重定向到 validate.jsp
标签: java jsp tomcat servlets jdbc