Servlet编写网页并与数据库连接

首先要保证电脑已经安装tomcat 和mysql并用myeslipse(esplise)实现

//首页 登陆页面 Login.java

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 {
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
/**
 * 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("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
    out.print("<input type='submit' value='提交'>");
    out.print("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
    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();
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90

}
这里写图片描述
//这个是用servlet 写的。

这里写图片描述

链接数据库 UserCoon.java

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";
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
       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;


   }

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

}

UserDao.java 各种方法

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();
        }

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
    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;

}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103

}

servlet各个方法

这里写图片描述

按提交键就会调用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 {
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
/**
 * 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 );
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

}

这里写图片描述

//查找方法 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 {
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
/**
 * 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>");
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77

}

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 {
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
/**
 * 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");

    }

}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

}

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();
    }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
/**
 * 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

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
h(Exception e){
            e.printStackTrace();
 
 
  • 1
  • 2
    }
}

 
 
  • 1
  • 2

}

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 {
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
/**
 * 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("更新成功");
    }
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

}

//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;
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值