页面交互(二)
我们现在可以写一个简单的页面,实现一下登录和注册功能
登录成功之后可以跳转到列表页面,查看用户的基本信息:
这些都是我们需要的:
代码示例:
LoginServlet :
package org.lianghs.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.lianghs.util.DBHelper;
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// 1. 字符编码
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html");
String userName = req.getParameter("userName");
String pwd = req.getParameter("pwd");
System.out.println(userName + ":" + pwd);
boolean bool = false;
// jdbc
Connection conn = DBHelper.conn();
String sql = "select * from users where user_name = ? and pwd = ?";
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
ps.setString(1, userName);
ps.setString(2, pwd);
ResultSet rs = ps.executeQuery();
if (rs.next())
bool = true;
else
bool = false;
} catch (SQLException e) {
e.printStackTrace();
}
DBHelper.close(conn, ps, null);
// 发送数据
PrintWriter out = resp.getWriter();
if (bool) {
resp.sendRedirect("list.jsp");
} else {
resp.sendRedirect("index.jsp");
out.flush();
out.close();
}
}
}
RegServlet :
package org.lianghs.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.lianghs.util.DBHelper;
public class RegServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1. 字符编码
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
//
response.setContentType("text/html");
// 获取数据
String userName = request.getParameter("userName");
String name = request.getParameter("name");
String pwd = request.getParameter("pwd");
String ageStr = request.getParameter("age");
Integer age = Integer.valueOf(ageStr);
System.out.println(userName + ":" + pwd + ":" + name + ":" + age);
boolean bool = false;
// jdbc
Connection conn = DBHelper.conn();
String sql = "insert into users (user_name,pwd,name,age) values (?,?,?,?)";
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
ps.setString(1, userName);
ps.setString(2, pwd);
ps.setString(3, name);
ps.setInt(4, age);
int count = ps.executeUpdate();
if (count > 0)
bool = true;
else
bool = false;
} catch (SQLException e) {
e.printStackTrace();
}
DBHelper.close(conn, ps, null);
// 发送数据
PrintWriter out = response.getWriter();
if (bool)
response.sendRedirect("index.jsp");
else
response.sendRedirect("reg.jsp");
out.flush();
out.close();
}
}
数据库基本信息:
DBHelper : 连接了SQL server数据库
package org.lianghs.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBHelper {
static {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection conn() {
Connection conn = null;
try {
conn = DriverManager.getConnection(
"jdbc:sqlserver://localhost:1433;databaseName=MyDB", "sa",
"1");
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void close(Connection conn, PreparedStatement ps, ResultSet rs) {
try {
if (conn != null)
conn.close();
if (ps != null)
ps.close();
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>org.lianghs.servlet.LoginServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>regServlet</servlet-name>
<servlet-class>org.lianghs.servlet.RegServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>regServlet</servlet-name>
<url-pattern>/regServlet</url-pattern>
</servlet-mapping>
</web-app>
index.jsp:
<%@ page language="java" import="java.util.*" 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>My JSP 'index.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">
<style type="text/css">
form {
margin: 0 auto;
}
</style>
</head>
<body>
<form action="login" method="post">
<h1>
登录页面
</h1>
用户名:
<input type="text" name="userName">
<br>
密码:
<input type="password" name="pwd">
<br>
<input type="submit" value="登录">
<button>
<a href="reg.jsp">注册</a>
</button>
</form>
</body>
</html>
list.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="org.lianghs.util.DBHelper"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.Connection"%>
<%
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">
<style type="text/css">
table,tr,td,th {
border: 1px solid black;
margin: 0 auto;
border-spacing: 0px;
padding: 10px;
}
</style>
</head>
<body>
<table>
<tr><th>编号</th><th>用户名</th><th>密码</th><th>姓名</th><th>年龄</th></tr>
<%
Connection conn = DBHelper.conn();
String sql = "select * from users";
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
%>
<tr><td><%= rs.getInt("id") %></td><td><%=rs.getString("user_name") %></td><td><%=rs.getString("pwd") %></td><td><%=rs.getString("name") %></td><td><%=rs.getInt("age") %></td></tr>
<%
}
} catch (SQLException e) {
e.printStackTrace();
}
DBHelper.close(conn, ps, null);
%>
</table>
</body>
</html>
regServlet.jsp :
<%@ page language="java" import="java.util.*" 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>My JSP 'regServlet.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">
<style type="text/css">
form {
margin: 0 auto;
}
</style>
</head>
<body>
<form action="regServlet" method="post">
<h1>
注册页面
</h1>
用户名:
<input type="text" name="userName">
<br>
密码:
<input type="password" name="pwd">
<br>
确认密码:
<input type="password" name="pwd">
<br>
姓名:
<input type="text" name="name">
<br>
年龄:
<input type="text" name="age">
<br>
<input type="submit" value="注册">
<button>
<a href="index.jsp">登录</a>
</button>
</form>
</body>
</html>
部署运行 之后,在浏览器上输入本机地址+文件地址 就可以看到效果了。
比如我的地址: 本机IP地址:localhost + 端口号 :8080 +项目名 Lession + jsp页面名 index.jsp