1:javabean
package cn.com.itsource;
public class User {
private String userid;//账号
private String name;//真实名字
private String password;//密码
public User(){}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
2:jsp(登录页面)
<%@ page contentType="text/html;charset=UTF-8" language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
<title></title>
</head>
<body>
<div align="center">
<h2>欢迎访问本论坛</h2>
<form action="/chapter1/TalkServlet" method="post">
<table>
<tr>
<td>账号:</td>
<td><input type="text" name="userid"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="提交"/></td>
</tr>
</table>
</form>
</div>
</body>
</html>
3:mxl文件映射提交给Servlet
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>chapter1</display-name>
<servlet>
<servlet-name>TalkServlet</servlet-name>
<servlet-class>cn.com.itsource.TalkServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TalkServlet</servlet-name>
<url-pattern>/TalkServlet</url-pattern>
</servlet-mapping>
4:Servlet(从数据库中查找并验证)转发给jsp(论坛页面)
package cn.com.itsource;
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;
public class TalkServlet extends HttpServlet {
private Connection conn=null;
private PreparedStatement pstmt=null;
private ResultSet rs=null;
private DBConnection jdbc=null;
private String sql=null;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String userid=request.getParameter("userid");
String password=request.getParameter("password");
String name=doCheckUser(userid, password);
if(name!="")
{
request.getSession().setAttribute("name",name);
request.getRequestDispatcher("/day1/post.jsp").forward(request, response);
}
else
{
response.sendRedirect("/day1/enter.jsp");
}
}
public String doCheckUser(String userid,String password)
{
try {
jdbc=new DBConnection();
conn=jdbc.getConnectoion();
try {
sql="select * from user where userid=? AND password=?";
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, userid);
pstmt.setString(2, password);
rs=pstmt.executeQuery();
if(rs.next())
{
return rs.getString(2);
}
else
return "";
} catch (SQLException e) {
e.printStackTrace();
}
} catch (Exception e1) {
e1.printStackTrace();
}
return "";
}
}
5:jsp(论坛编写页面)
<%@ page contentType="text/html;charset=UTF-8" language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
<title>模拟论坛发帖功能</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
String name=(String)request.getAttribute("name");
%>
<h2 align="right"><%=name%>的论坛页面</h2>
<div align="center">
<form action="/chapter1/day1/show.jsp" method="post">
<table>
<tr>
<td>主题:</td>
<td><textarea rows="3" cols="30" name="theme"></textarea>
</tr>
<tr>
<td>内容:</td>
<td><textarea rows="10" cols="50" name="content"></textarea>
</tr>
<tr>
<td colspan="2"><input type="submit" value="提交"/></td>
</tr>
</table>
</form>
</div>
</body>
</html>
6:jsp(论坛显示页面)
<%@ page contentType="text/html;charset=UTF-8" language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
<title>模拟论坛发帖功能</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
String name=(String)request.getParameter("name");
String theme=(String)request.getParameter("theme");
String content=(String)request.getParameter("content");
request.getSession().setAttribute("name",name);
%>
<h2 align="right"><%=name%>的论坛内容</h2>
<div align="center">
<form action="show.jsp" method="post">
<table>
<tr>
<td>主题:</td>
<td><textarea rows="3" cols="30" name="theme" value="<%=theme%>"></textarea>
</tr>
<tr>
<td>内容:</td>
<td><textarea rows="10" cols="50" name="content" value="<%=content%>"></textarea>
</tr>
<tr>
<td colspan="2"><a href="post.jsp">返回</a> <a href="enter.jsp">退出</a>
</td>
</tr>
</table>
</form>
</div>
</body>
</html>