index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="com.hanqi.jdbc.MethodDal,java.util.List,com.hanqi.jdbc.Meseeage" %>
<!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=UTF-8">
<title>Insert title here</title>
<style type="text/css">
*{
margin:0px auto;
padding:0px;
}
.a{
width:100%;
height:100%;
margin-left:450px;
}
</style>
</head>
<body>
<h1>这是首页</h1>
<div style="width:500px;height:500px">
<form action="IndexServlet" method="post">
<textarea rows="20" cols="50" name="wenben" ></textarea><br>
<input type="submit" name="tijiao" value="提交留言" />
</form>
</div>
<%
MethodDal m =new MethodDal();
List<Meseeage> list=m.SelectList();
for(int i=0;i<list.size();i++){
out.print("<div class=a>"+"用户:"+list.get(i).getUsername()+" "+list.get(i).getTime()+"<br>"
+"留言:"+list.get(i).getContext()+"<br>"+"</div>");
}
%>
</body>
</html>
Resist.jsp
<body>
<h1>这是注册页面</h1>
<form action="ResistServlet" method="post" >
<input type="text" name="username" placeholder="请输入用户名"/><br>
<input type="text" name="password" placeholder="请输入密码"/><br>
<input type="text" name="password1" placeholder="请再次输入密码"/><br>
<input type="submit" value="注册"/><a href="login.jsp">超级链接</a>
</form>
</body>
Message.jsp
<body>
<%
String code=request.getParameter("code");
if("1".equals(code)){
out.print("<h1>注册成功!</h1>");
}if("2".equals(code)){
out.print("<h1>信息输入不完整!</h1>");
}if("3".equals(code)){
out.print("<h1>输入的密码不一致!</h1>");
}if("4".equals(code)){
out.print("<h1>用户名错误!</h1>");
}if("5".equals(code)){
out.print("<h1>用户不存在!</h1>");
}if("6".equals(code)){
out.print("<h1>密码错误!</h1>");
}if("7".equals(code)){
out.print("<h1>留言成功!</h1>");
}
%>
</body>
Login.jsp
<body>
<h1>这是登陆页面</h1>
<form action="LoginServlet" method="post" >
<input type="text" name="username" placeholder="请输入用户名"/><br>
<input type="text" name="password" placeholder="请输入密码"/><br>
<a href="index.jsp"><input type="submit" value="登陆"/></a>
<a href="resist.jsp">注册</a>
</form>
</body>
LogServler.java
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
String username=request.getParameter("username");
String password=request.getParameter("password");
MethodDal md=new MethodDal();
if(password.equals(md.SelectData(username))){
response.sendRedirect("index.jsp");
request.getSession().setAttribute("username", username);
}else{response.sendRedirect("message.jsp?code=6");
}
}
ResistServer.java
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
String password1 = request.getParameter("password1");
if(CheckParm(username,password,password1)){
if(password.equals(password1)){
MethodDal md=new MethodDal();
User u=new User(username,password);
md.InsertData(u);
response.sendRedirect("login.jsp");
}else{
response.sendRedirect("message.jsp?code=3");
}
}else{
response.sendRedirect("message.jsp?code=2");
}
}
User.java
public class User {
public String username;
public String password;
public User(){}
public User(String username, String password) {
super();
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Message.java
import java.util.Date;
public class Meseeage {
public String username;
public String context;
public Date time;
public Meseeage(){}
public Meseeage(String username, String context, Date time) {
super();
this.username = username;
this.context = context;
this.time = time;
}
@Override
public String toString() {
return "username + time +'\n'+context";
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
}
JdbcTextConn.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcTextConn {
private static final String USERNAME="text";
private static final String PASSWORD="text";
private static final String URL="jdbc:oracle:thin:@localhost:1521:xe";
private static final String DRIVERCLASSNAME="oracle.jdbc.OracleDriver";
public static Connection getConnection(){
Connection conn=null;
try {
Class.forName(DRIVERCLASSNAME);
conn=DriverManager.getConnection(URL,USERNAME,PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void destory(Connection conn,Statement sm,ResultSet rs){
if(conn!=null){
try {
conn.close();
conn=null;
} catch (SQLException e) {
e.printStackTrace();
}
}
if(sm!=null){
try {
sm.close();
sm=null;
} catch (SQLException e) {
e.printStackTrace();
}
}
if(rs!=null){
try {
rs.close();
rs=null;
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args){
System.out.println(getConnection());
}
}
MethodDal.java
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class MethodDal {
private Connection con;
private PreparedStatement pas;//控制sql语句
private ResultSet rs;//查询结果的
public int InsertData(User u){
init();
int i=-1;
String sql="insert into RESIST values(?,?)";
try {
pas=con.prepareStatement(sql);
pas.setString(1, u.username);
pas.setString(2, u.password);
i=pas.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
close();
return i;
}
public String SelectData(String username){
String sql="select r.password from resist r where r.username=? ";
init();
String s=null;
try {
pas=con.prepareStatement(sql);
pas.setString(1, username);
rs=pas.executeQuery();
while(rs.next()){
s=rs.getString("password");
}
} catch (SQLException e) {
e.printStackTrace();
}
close();
return s;
}
public int Insertcon(Meseeage m){
init();
int i=-1;
String sql="insert into liuyan values(?,?,sysDate)";
try {
pas=con.prepareStatement(sql);
pas.setString(1, m.username);
pas.setString(2, m.context);
i=pas.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
close();
return i;
}
public List<Meseeage> SelectList(){
String sql="select * from liuyan";
init();//初始化
List<Meseeage> list= new ArrayList<Meseeage>();
try {
pas=con.prepareStatement(sql);
rs=pas.executeQuery();
while(rs.next()){
Meseeage m=new Meseeage();
m.setUsername(rs.getString(1));
m.setContext(rs.getString(2));
m.setTime(rs.getDate(3));
list.add(m);
}
} catch (SQLException e) {
e.printStackTrace();
}
close();
return list;
}
public void init(){
con=JdbcTextConn.getConnection();
}public void close(){
JdbcTextConn.destory(con, pas, rs);
}
}
IndexServler.java
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
String context=request.getParameter("wenben");
HttpSession session=request.getSession();
String u=(String)session.getAttribute("username");//获取登陆的用户名
MethodDal md= new MethodDal();
List<Meseeage> list=new ArrayList<Meseeage>();
Meseeage m=new Meseeage(u,context,new Date());
if(u==null){
response.sendRedirect("login.jsp");
}else{
if(context!=null){
md.Insertcon(m);
if(md.Insertcon(m)==1){
response.sendRedirect("index.jsp");
}
}else{
System.out.println("内容为空");
}
}
}