开发环境:idea+mysql
使用技术:jsp+servlet
关键代码:
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>nchu</groupId>
<artifactId>hospital</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>hospital Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20160810</version>
</dependency>
<!-- java邮件 -->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.6</version>
</dependency>
</dependencies>
<build>
<finalName>hospital</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
@WebServlet("/admin/doctorManage")
public class DoctorManage extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String office = Util.nullToString(req.getParameter("office"));
String name = Util.nullToString(req.getParameter("doctor"));
String action = Util.nullToString(req.getParameter("action"));
if("add".equals(action)){
String message="增加医生失败!";
DoctorDao doctorDao = new DoctorDao();
System.out.println(req.getParameter("account"));
List<Doctor> doctors = doctorDao.query("where account=? ",new Object[]{Util.nullToString(req.getParameter("account"))});
if(doctors.size()==0){
Doctor doctor=new Doctor();
doctor.setAccount(req.getParameter("account"));
doctor.setPassword(req.getParameter("password"));
doctor.setDname(req.getParameter("name"));
doctor.setFee(req.getParameter("fee"));
doctor.setGender(req.getParameter("gender"));
doctor.setAge(req.getParameter("age"));
doctor.setOffice(req.getParameter("office1"));
doctor.setRoom(req.getParameter("room"));
doctor.setCareer(req.getParameter("career"));
doctor.setDescription(req.getParameter("description"));
doctor.setPicpath(req.getContextPath()+"/images/docpic/default.jpg");
if(doctorDao.insert(doctor)){
message="添加"+req.getParameter("name")+"医生成功!";
}
}else {
message=req.getParameter("account")+"账号已存在!";
}
req.setAttribute("message",message);
}
int start = Util.nullToZero(req.getParameter("start"));
DoctorDao doctorDao=new DoctorDao();
String where="where office like ? and dname like ? ";
int total=doctorDao.getDoctorCount(where,new Object[]{Util.toLike(office),Util.toLike(name)});
Pages pages = new Pages(start , total, 6);
where+="limit "+((pages.getCurrentPage()-1)*6)+",6";
List<Doctor> doctors = doctorDao.query(where, new Object[]{Util.toLike(office),Util.toLike(name)});
req.setAttribute("doctors",doctors);
req.setAttribute("pages",pages);
req.setAttribute("doctor",name);
//OfficeDao officeDao=new OfficeDao();
//List<Office> offices = officeDao.query("officename", office, "");
req.setAttribute("office",office);
req.getRequestDispatcher("doctorManage.jsp").forward(req,resp);
}
}