[JavaWeb基礎] 004.用JSP + SERVLET 進行簡單的增加刪除修改

來源:互聯網
上載者:User

標籤:

  上一次的文章,我們講解了如何用JAVA訪問MySql資料庫,對資料進行增加刪除修改查詢。那麼這次我們把具體的頁面的資料庫操作結合在一起,進行一次簡單的學生資訊操作案例。

  首先我們建立一個專門用於學生管理的ManageServlet。

  

  接著我們需要一個展現資料的頁面,也就是 UserList.jsp

<%@page import="com.babybus.sdteam.vo.Student"%><%@ page language="java" import="java.util.*" pageEncoding="GB18030"%><%  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>學生資訊管理</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"><link rel="stylesheet" type="text/css" href="styles.css">  </head><body> <%=basePath %> <table border="1" width="100%" > <tr><th>uid</th><th>學生姓名</th><th>年齡</th><th>班級</th><th>操作</th></tr> <% // 擷取學生列表資料 List<Student> lsUser=(List)request.getAttribute("students"); if(lsUser==null||lsUser.size()==0){   pageContext.setAttribute("theUser","1"); }else{    // 遍曆列表產生表格式資料   for(int i=0;i<lsUser.size();i++){     pageContext.setAttribute("theUser",lsUser.get(i)); %> <tr> <td>${theUser.id} </td><td>${theUser.studentname} </td><td>${theUser.age} </td><td>${theUser.classname} </td> <td><a href="AddUser.jsp?id=${theUser.id}">修改<a/>| <a href="Mangage?method=del&userid=${theUser.id}" onclick="return confirm(‘確定要刪除嗎?‘)" }>刪除</a></td> </tr> <%    } } %> </table>               <a href="AddUser.jsp">增加<a/>  </body></html>

  接下來我們需要一個進行增加修改的頁面AddUser.jsp

<%@page import="com.babybus.sdteam.bo.ManageServlet"%><%@page import="com.babybus.sdteam.vo.Student"%><%@page import="com.babybus.sdteam.bo.LoginServlet"%><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";response.setContentType("text/html;charset=UTF-8");request.setCharacterEncoding("UTF-8");%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>My JSP ‘AddUser.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="學生資訊管理"><link rel="stylesheet" type="text/css" href="styles.css">  </head>    <body>    <%    String id= request.getParameter("id");    if(id!=null){        ManageServlet loginServlet =new ManageServlet();                  Student student = loginServlet.getStudentById(id);       pageContext.setAttribute("theUser",student);         pageContext.setAttribute("method","update");          pageContext.setAttribute("title","修改");      }else{pageContext.setAttribute("method","add");  pageContext.setAttribute("title","添加");       }%><form action="Mangage?method=${method}" method="post" ><input type="hidden" name="userid" value="${theUser.id}"><table width="100%" height="200px" border="1"><tr><td colspan="2">${title}</td></tr><tr><td align="right">學生名</td><td> <input type="text" name="studentname" value="${theUser.studentname}"></td></tr><tr><td align="right">年齡:</td><td> <input type="text" name="age" value="${theUser.age}"></td></tr><td align="right">班級:</td><td> <input type="text" name="classname" value="${theUser.classname}"></td></tr><tr><td colspan=‘2‘ align="center"><input type="submit" value="儲存" name="btnSubmit">   <input type="reset" value="重設" name="btnCancel"></td></tr></table></form></body></html>

  有了頁面支撐,我們需要背景Servlet進行資料處理轉寄

/*** 業務處理* @param req* @param rep* @throws IOException * @throws ServletException */public void process(HttpServletRequest req,HttpServletResponse rep) throws ServletException, IOException{  String method = req.getParameter("method");  System.out.println("操作:" + method);  Student student = new Student();  if(!"del".equals(method))  {    // 進行編碼轉換,避免中文亂碼    String studentname = new String(req.getParameter("studentname").getBytes("ISO-8859-1"),"utf-8");    int            age = Integer.parseInt(req.getParameter("age"));    String   classname = new String(req.getParameter("classname").getBytes("ISO-8859-1"),"utf-8");    student.setStudentname(studentname);    student.setAge(age);    student.setClassname(classname);  }  StudentDao dao = new StudentDao();  // 修改  if("update".equals(method))   {    String id = req.getParameter("userid");    student.setId(Integer.parseInt(id));    try {      dao.updateStudent(student);    } catch (SQLException e) {      e.printStackTrace();    }  }  // 增加  else if("add".equals(method))  {    try {    dao.insertStudent(student);    } catch (SQLException e) {      e.printStackTrace();    }  }  // 刪除  else if("del".equals(method))  {    try {      String id = req.getParameter("userid");      dao.deleteStudent(Integer.parseInt(id));    } catch (SQLException e) {      e.printStackTrace();    }  }
  ManageServlet manageservlet = new ManageServlet();  // 擷取全部列表  List<Student> resultlist = manageservlet.getStudentByCondition(null);  req.setAttribute("students", resultlist);  RequestDispatcher dispatcher = req.getRequestDispatcher("UserList.jsp");  dispatcher.forward(req, rep); }

  上面就是全部的增加刪除修改的代碼。下面給出兩個頁面的效果

 

  通過上面的例子我們就可以很好的掌握了J2EE的入門

 

結語

  • 受益,掌握了J2EE的入門

 

 

本站文章為 寶寶巴士 SD.Team 原創,轉載務必在明顯處註明:(作者官方網站: 寶寶巴士 ) 

轉載自【寶寶巴士SuperDo團隊】 原文連結: http://www.cnblogs.com/superdo/p/4579275.html

 

[JavaWeb基礎] 004.用JSP + SERVLET 進行簡單的增加刪除修改

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.