提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
一、第一个servlet
1.在MyEclipse中新建一个Web项目
2.在包名上右键 new->servlet
3.将项目部署到Tomcat
4.在浏览器上输入 localhost:8080/Work01 (8080为端口号,Work01:项目名称)
二、Servlet 请求响应 页面跳转
需求
–注册页面添加登录链接
–登录页面添加注册链接
–注册成功跳转到登录页面
–注册失败跳转到注册页面
–登录成功跳转到用户列表页面(这个需要重新写- -套查询的代码)
–登录失败跳转到登录页面
1.注册页面
package org.maoyuanhao;
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.maoyuanhao.util.DButil;
public class InsertServlet 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");
// 2.解决响应编码问题
response.setCharacterEncoding("utf-8");
// 3.设置响应的格式
response.setContentType("text/html");
// 2.获取页面的值
String userName = request.getParameter("userName");
String pwd = request.getParameter("pwd");
String name = request.getParameter("name");
String ageStr = request.getParameter("age");
Integer age = Integer.parseInt(ageStr);
// 注册业务 -- 新增 -INSERT INTO student(user_name,pwd,name,age) values(?,?,?,?)
// 工具类 DButil
// 1.加载驱动程序
Connection conn = DButil.getConn();
// 2.准备一个sql语句
String sql = "insert into student(user_name,pwd,name,age) values (?,?,?,?)";
PreparedStatement ps = null;
boolean isTrue = false;
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) {
isTrue = true;
} else {
isTrue = false;
}
} catch (SQLException e) {
e.printStackTrace();
}
// 关闭流
DButil.close(conn, ps, null);
if (isTrue) {
//注册成功跳转到登陆页面
response.sendRedirect("selete.jsp");
} else {
//注册失败跳转回注册页面
response.sendRedirect("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">
.servlet {
font-size: 25pt;
color: #FF0000;
}
</style>
</head>
<body>
<form action="insertServlet" method="post">
<table border="1" cellspacing="1" cellpadding="1">
<caption class="servlet">注册页面</caption>
<tr>
<td>用户名:</td>
<td><input type="text" name="userName" />
</td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="pwd" />
</td>
</tr>
<tr>
<td>确认密码:</td>
<td><input type="password" name="pwd2" />
</td>
</tr>
<tr>
<td>姓名:</td>
<td><input type="text" name="name" />
</td>
</tr>
<tr>
<td>年龄:</td>
<td><input type="text" name="age" />
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="注册" /><input
type="reset" value="重置" />
</td>
</tr>
</table>
</form>
<a href="selete.jsp">已有帐号,去登录</a>
</body>
</html>
2.登陆页面
package org.maoyuanhao;
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.maoyuanhao.util.DButil;
public class SelectServlet 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");
// 2.解决响应编码问题
response.setCharacterEncoding("utf-8");
// 3.设置响应的格式
response.setContentType("text/html");
// 2.处理请求
String userName = request.getParameter("userName");
String pwd = request.getParameter("pwd");
// 工具类 DButil
// 1.加载驱动程序
Connection conn = DButil.getConn();
// 2.准备一个sql语句
String sql = "select * from student where user_name =? and pwd = ?";
PreparedStatement ps = null;
boolean isTrue = false;
try {
ps = conn.prepareStatement(sql);
ps.setString(1, userName);
ps.setString(2, pwd);
ResultSet rs = ps.executeQuery();
if(rs.next()){
isTrue = true;
}else{
isTrue = false;
}
} catch (SQLException e) {
e.printStackTrace();
}
// 关闭流
DButil.close(conn, ps, null);
//处理响应
if (isTrue) {
//登录成功跳转用户列表页面
response.sendRedirect("createServlet");
} else {
//登录失败跳转回登录页面
response.sendRedirect("selete.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">
.servlet {
font-size: 25pt;
color: #FF0000;
}
</style>
</head>
<body>
<form action="selectServlet" method="post">
<table border="1" cellspacing="1" cellpadding="1">
<caption class="servlet">登录页面</caption>
<tr>
<td>用户名:</td>
<td><input type="text" name="userName" />
</td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="pwd" />
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="登录" />
</td>
</tr>
</table>
</form>
<a href="index.jsp">未有帐号,去注册</a>
</body>
</html>
3.列表页面
package org.maoyuanhao;
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 java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.maoyuanhao.util.DButil;
public class CreateServlet 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");
// 2.解决响应编码问题
response.setCharacterEncoding("utf-8");
// 3.设置响应的格式
response.setContentType("text/html");
// 1.加载驱动程序
Connection conn = DButil.getConn();
// 响应
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE>");
out.println("</HEAD>");
out.println("<BODY>");
out.println("<table>");
out.println("<tr><th>ID</th><th>用戶名</th><th>密碼</th><th>姓名</th><th>年龄</th></tr>");
// jdbc
// 2.准备一个sql语句
String sql = "select * from student";
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
out.println("<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();
}
// 关闭流
DButil.close(conn, ps, rs);
out.println("</table>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
列表功能的页面代码
<%@ 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">
</head>
<body>
<form action="createServlet" method="post"></form>
</body>
</html>
8.运行结果
总结
撑不住的时候,可以对自己说声“我好累”,但永远不要在心里承认说“我不行”。