Last time, we explained how to use Java to access MySQL database, to increase the deletion of data to modify the query . So this time we combine the database operations of specific pages to conduct a simple case of student information Operation .
First we create a Manageservletthat is dedicated to student management .
Then we need a page that shows the data, which is 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" > Next we need a page 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" > With page support, we need the background of servlet data processing forwarding
/*** Business Processing * @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 ("Operation:" + method); Student Student = new Student (); if (! " Del ". Equals (method)) {//Encode conversion, avoid Chinese garbled string studentname = new String (Req.getparameter (" Studentname "). GetBytes (" I So-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 (); Modify if ("Update". Equals (method)) {String id = req.getparameter ("userid"); Student.setid (Integer.parseint (id)); try {dao.updatestudent (student); } catch (SQLException e) {e.printstacktrace (); }}//Add ElsE if ("Add". Equals (method)) {try {dao.insertstudent (student); } catch (SQLException e) {e.printstacktrace (); }}//Remove 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 (); Get the full list list<student> resultlist = manageservlet.getstudentbycondition (null); Req.setattribute ("Students", resultlist); RequestDispatcher dispatcher = Req.getrequestdispatcher ("userlist.jsp"); Dispatcher.forward (req, rep); }
Above is all the added code to delete the changes. The following gives the effect of two pages
From the above example, we can get a good grasp of the introduction of the Java EE
Conclusion
- Benefited, mastered the introduction of the Java EE
This site article is for baby bus SD. Team Original, reproduced must be clearly noted: (the author's official website: Baby bus )
Reprinted from "Baby bus Superdo Team" original link: http://www.cnblogs.com/superdo/p/4579275.html
[Javaweb Basic] 004. Use JSP + SERVLET to make simple additions and deletions