首先要保证电脑已经安装tomcat 和mysql并用myeslipse(esplise)实现
package nuc.exam.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Login extends HttpServlet {
/**
* Constructor of the object.
*/
public Login() {
super();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>登陆界面</TITLE></HEAD>");
out.println(" <BODY>");
out.print("<form action='Insert' method='post'>");
out.print("<table border='1' align='center'>");
out.print("<tr align='center'>");
out.print("<td colspan='2'>");out.print("<h2>电影介绍</h2>");out.print("</td>");
out.print("</tr>");
out.print("<tr align='center'>");
out.print("<td>");out.print("电影名称:");out.print("</td>");
out.print("<td>");out.print("<input type='text' name='name' value=''>");out.print("</td>");
out.print("</tr>");
out.print("<tr align='center'>");
out.print("<td>");out.print("电影价格:");out.print("</td>");
out.print("<td>");out.print("<input type='text' name='price' value=''>");out.print("</td>");
out.print("</tr>");
out.print("<tr align='center'>");
out.print("<td>");out.print("电影产地:");out.print("</td>");
out.print("<td>");
out.print("<input type='radio' name='from' value='大陆'>大陆");
out.print("<input type='radio' name='from' value='中国香港'>中国香港");
out.print("<input type='radio' name='from' value='国外'>国外");
out.print("</td>");
out.print("</tr>");
out.print("<tr align='center'>");
out.print("<td>");out.print("电影类型:");out.print("</td>");
out.print("<td>");
out.print("<select name='type'>");
out.print("<option>动作片</option>");
out.print("<option>动画片</option>");
out.print("<option>剧情片</option>");
out.print("</select>");
out.print("</td>");
out.print("</tr>");
out.print("<tr align='center'>");
out.print("<td>");out.print("电影主演:");out.print("</td>");
out.print("<td>");
out.print("<input type='checkbox' name='actor' value='李小龙'>李小龙");
out.print("<input type='checkbox' name='actor' value='成龙'>成龙");
out.print("<input type='checkbox' name='actor' value='其他'>其他");
out.print("</td>");
out.print("</tr>");
out.print("<tr align='center'>");
out.print("<td>");out.print("电影简介:");out.print("</td>");
out.print("<td height='100px'>");
out.print("<input type='textarea' style='height:100px;' name='' value=''> ");
out.print("</td>");
out.print("</tr>");
out.print("<tr align='center'>");
out.print("<td>");out.print("电影详细介绍:");out.print("</td>");
out.print("<td>");
out.print("<input type='file' name='' calue=''>");
out.print("</td>");
out.print("</tr>");
out.print("<tr>");
out.print("<td colspan='2'>");
out.print(" ");
out.print("<input type='submit' value='提交'>");
out.print(" ");
out.print("<input type='reset' value='重置'>");
out.print("</td>");
out.print("</tr>");
out.print("</table>");
out.print("</form>");
out.println("");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
//这个是用servlet 写的。
package nuc.test.coon;
import java.sql.*;
public class UserCoon {
public static final String DBDRIVER="org.gjt.mm.mysql.Driver";
public static final String DBURL="jdbc:mysql://localhost:3306/test";
public static final String DBUSER="root";
public static final String DBPASS="123456";
Connection conn=null;
public Connection getCoon(){
try {
Class.forName(DBDRIVER);
conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
**Find.java**各种方法
package nuc.test.Dao;
import java.sql.*;
import java.util.*;
import nuc.test.coon.*;
import nuc.test.user.User;
public class Find {
public int SelectCount(){
int count=0;
ResultSet rs=null;
UserCoon coon=new UserCoon();
Connection coona=coon.getCoon();
String sql_Counts="select count(*) from user";
try {
PreparedStatement pst=coona.prepareStatement(sql_Counts);
rs=pst.executeQuery();
if(rs.next()){
count=rs.getInt(1);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return count;
}
//分页查询
public List<User> Selectcontent(int page){
List<User> list=new ArrayList<User>();
UserCoon coon=new UserCoon();
Connection coona=coon.getCoon();
String sql_Count="select * from user limit ?,?";
try {
PreparedStatement pst=coona.prepareStatement(sql_Count);
pst.setInt(1, (page-1)*User.PAGESIZE);
pst.setInt(2, User.PAGESIZE);
ResultSet rs=pst.executeQuery();
while(rs.next()){
User usera=new User();
usera.setId(rs.getString("id"));
usera.setUsername(rs.getString("username"));
usera.setPassword(rs.getString("password"));
usera.setType(rs.getString("type"));
list.add(usera);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
}
package nuc.test.Dao;
import java.sql.*;
import nuc.test.coon.*;
import nuc.test.user.*;
//插入
public class UserDao {
public int Insert(User usera){
PreparedStatement pst=null;
int rs=0;
UserCoon coon=new UserCoon();
Connection coona=coon.getCoon();
String sql_insert="insert into user(username,password,type) values(?,?,?)";
try{
pst=coona.prepareStatement(sql_insert);
pst.setString(1, usera.getUsername());
pst.setString(2, usera.getPassword());
pst.setString(3,usera.getType());
rs=pst.executeUpdate();
}catch(Exception e){
e.printStackTrace();
}
return rs;
}
//查询
public ResultSet Query(){
PreparedStatement pst=null;
ResultSet rs=null;
UserCoon coon=new UserCoon();
Connection coona=coon.getCoon();
String sql_query="select * from user";
try {
pst=coona.prepareStatement(sql_query);
rs=pst.executeQuery();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
//查找
public ResultSet Select(User usera){
PreparedStatement pst=null;
ResultSet rs=null;
UserCoon coon=new UserCoon();
Connection conna=coon.getCoon();
String sql_Select="select * from user where id=?";
try {
pst=conna.prepareStatement(sql_Select);
pst.setString(1, usera.getId());
rs=pst.executeQuery();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
//更新
public int Update(User usera){
PreparedStatement pst=null;
int rs=0;
UserCoon coon=new UserCoon();
Connection coona=coon.getCoon();
String sql_update="update user set username=?,password=?,type=? where id=?";
try {
pst=coona.prepareStatement(sql_update);
pst.setString(1, usera.getUsername());
pst.setString(2, usera.getPassword());
pst.setString(3, usera.getType());
pst.setString(4, usera.getId());
rs=pst.executeUpdate();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
//删除
public int Delete(User usera){
PreparedStatement pst=null;
int rs=0;
UserCoon coon=new UserCoon();
Connection coona=coon.getCoon();
String sql_delete="delete from user where id=?";
try {
pst=coona.prepareStatement(sql_delete);
pst.setString(1, usera.getId());
rs=pst.executeUpdate();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
//查找某一项数据 可用来更新
public ResultSet Login(User usera){
PreparedStatement pst=null;
ResultSet rs=null;
UserCoon coon=new UserCoon();
Connection conna=coon.getCoon();
String sql_Select="select * from user where username=? and password=? and type=?";
try {
pst=conna.prepareStatement(sql_Select);
pst.setString(1, usera.getUsername());
pst.setString(2, usera.getPassword());
pst.setString(3, usera.getType());
rs=pst.executeQuery();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
}
按提交键就会调用Insert方法
以下方法都是由网页jsp方法改写的,然后调用UserDao里的方法来实现
//插入方法 Insert.java
package nuc.test.servet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import nuc.test.Dao.UserDao;
import nuc.test.user.User;
public class Insert extends HttpServlet {
/**
* Constructor of the object.
*/
public Insert() {
super();
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
request.setCharacterEncoding("utf-8");
User user=new User();
UserDao dao =new UserDao();
user.setUsername(request.getParameter("username"));
user.setPassword(request.getParameter("password"));
user.setType(request.getParameter("type"));
int a=dao.Insert(user);
if(a!=0){
out.print("插入成功");
}
response.setHeader("refresh", null );
}
}
//查找方法 ShowPage.java
package nuc.test.servet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import nuc.test.Dao.Find;
import nuc.test.user.User;
public class ShowPage extends HttpServlet {
/**
* Constructor of the object.
*/
public ShowPage() {
super();
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
request.setCharacterEncoding("utf-8");
out.print(" <table align='center' width='800' border='1'>");
out.print("<tr>");
out.print("<td align='center' colspan='5'>");
out.print("所有用户信息");
out.print("</td>");
out.print("</tr>");
out.print("<tr align='center'>");
out.print("<td>用户名</td><td>密码</td><td>用户类型</td><td colspan='2'>操作方法</td>");
out.print("</tr>");
int currpage=1;
if(request.getParameter("page")!=null){
currpage=Integer.parseInt(request.getParameter("page"));
}
Find find=new Find();
List<User> listall=new ArrayList<User>();
listall=find.Selectcontent(currpage);
Iterator<User> it=listall.iterator();
while(it.hasNext()){
User usera=it.next();
out.print("<tr align='center'>");
out.print("<td>"+usera.getUsername()+"</td>");
out.print("<td>"+usera.getPassword()+"</td>");
out.print("<td>"+usera.getType()+"</td>");
out.print("<td><a href='servlet/Update?id="+usera.getId()+"'>修改</a></td>");
out.print("<td><a href='servlet/Delete?id="+usera.getId()+"'>删除</a></td>");
out.print("</tr>");
}
out.print("<tr>");
out.print("<td align='center' colspan='5'>");
out.print("<jsp:include page='Bar.jsp'>");
out.print("</td>");
out.print("</tr>");
out.print("<tr align='center'><td colspan='5'><a hraf='first.jsp'>添加用户</a></td></tr>");
out.print("</table>");
}
}
Delete.java
package nuc.test.servet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import nuc.test.Dao.UserDao;
import nuc.test.user.User;
public class Delete extends HttpServlet {
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
User user=new User();
user.setId(request.getParameter("id"));
UserDao dao=new UserDao();
int result=dao.Delete(user);
if(result!=0){
out.println("删除成功");
//response.sendRedirect("servlet/FindPage");
}
}
}
update.java//先查出某一条记录,准备删除
package nuc.test.servet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import nuc.test.Dao.UserDao;
import nuc.test.user.User;
public class Update extends HttpServlet {
public Update(){
super();
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
request.setCharacterEncoding("utf-8");
User user=new User();
UserDao dao =new UserDao();
user.setId(request.getParameter("id"));
ResultSet result=dao.Select(user);
try{
if(result.next()){
out.println("<form action='doUpdate?id="+result.getString("id")+"' method='post'>");
out.println("用户名:<input type='text' value='"+result.getString("username")+"' name='username'>");
out.println("密 码:<input type='text' value='"+result.getString("password")+"' name='password'>");
out.println("用户类型<select>");
out.println("<option value='管理员'>管理员</option>");
out.println("<option value='普通用户'>普通用户</option>");
out.println("</select>");
out.println("<input type='submit' value='修改'>");
out.println("<input type='reset' value='取消'>");
out.println("</form>");
}
}catc
h(Exception e){
e.printStackTrace();
}
}
}
doupdate.java
package nuc.test.servet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import nuc.test.Dao.UserDao;
import nuc.test.user.User;
public class doUpdate extends HttpServlet {
/**
* Constructor of the object.
*/
public doUpdate() {
super();
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
request.setCharacterEncoding("utf-8");
User user=new User();
UserDao dao=new UserDao();
user.setId(request.getParameter("id"));
user.setUsername(request.getParameter("username"));
user.setPassword(request.getParameter("password"));
user.setType(request.getParameter("type"));
int result=0;
result=dao.Update(user);
if(result!=0){
out.print("更新成功");
}
}
}
//user.java源代码
public class User {
public static final int PAGESIZE=5;
private String id;
private String username;
private String password;
private String type;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}