Javaweb 学习
今天进行b及测试题的学习操作
完成一个项目5个多小时。
package servlet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.text.SimpleDateFormat;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import dao.myDao;
import domain.User;
/**
* Servlet implementation class AddServlet
*/
@WebServlet("/addServlet")
public class doServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public doServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void manager_longin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
String ID =request.getParameter("ID");
String password =request.getParameter("password");
String name = request.getParameter("name");
String type =request.getParameter("type");
System.out.println("学号是"+ID+"姓名"+name+"密码"+password+"类型"+type);
User user = new User(ID,name,password,type);
myDao cd = new myDao();
if(cd.manager_longin(user)) {
request.setAttribute("message","成功");
request.getRequestDispatcher("manager_login.jsp").forward(request,response);
}
else
{
request.setAttribute("message","失败");
request.getRequestDispatcher("manager_login.jsp").forward(request,response);
}
}
private void list(HttpServletRequest request, HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
HttpSession session = request.getSession();
String ID=(String)session.getAttribute("ID");
System.out.println("存入的ID为"+ID);
String cxfs=request.getParameter("cxfs");
String value=request.getParameter("value");
System.out.println("cxfs为"+cxfs+"value"+value);
if(cxfs.equals(""))
{
List<User> list=myDao.list();
System.out.println(list.toString());
request.setAttribute("list", list);
}
else if(cxfs.equals("ID"))
{
List<User> list=myDao.IDlist(value);
request.setAttribute("list", list);
}
else if(cxfs.equals("姓名"))
{
List<User> list=myDao.namelist(value);
request.setAttribute("list", list);
}
request.getRequestDispatcher("list.jsp").forward(request,response);
}
private void searchMyInformation(HttpServletRequest request, HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
HttpSession session = request.getSession();
String ID=(String)session.getAttribute("ID");
String myType=(String)session.getAttribute("type");
System.out.println("存入的ID为"+ID);
myDao cd = new myDao();
User user = cd.searchID(ID);
List<User> list=new ArrayList<>();
list.add(user);
request.setAttribute("list", list);
System.out.println("成功");
request.getRequestDispatcher("list_information.jsp").forward(request,response);
}
private void back(HttpServletRequest request, HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
HttpSession session = request.getSession();
String myType=(String)session.getAttribute("type");
System.out.println("type为"+myType);
request.setAttribute("message","返回主页");
if(myType.equals("1.员工"))
request.getRequestDispatcher("person.jsp").forward(request,response);
else if(myType.equals("2.教师"))
request.getRequestDispatcher("tercher.jsp").forward(request,response);
else
request.getRequestDispatcher("manager.jsp").forward(request,response);
}
private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
String ID = request.getParameter("ID");
String name = request.getParameter("name");
String password = request.getParameter("password");
String type = request.getParameter("type");
User bean=new User(ID,name,password,type);
System.out.println("ID"+ID+"姓名"+name);
if(myDao.update(bean))
{
request.setAttribute("message", "修改成功");
if(type.equals("1.员工"))
{
if(type.equals("1.员工"))
request.getRequestDispatcher("person.jsp").forward(request,response);
else if(type.equals("2.教师"))
request.getRequestDispatcher("tercher.jsp").forward(request,response);
else
request.getRequestDispatcher("manager.jsp").forward(request,response);
}
}
else
{
request.setAttribute("message", "修改失败");
if(type.equals("1.员工"))
request.getRequestDispatcher("person.jsp").forward(request,response);
else if(type.equals("2.教师"))
request.getRequestDispatcher("tercher.jsp").forward(request,response);
else
request.getRequestDispatcher("manager.jsp").forward(request,response);
}
}
private void searchID(HttpServletRequest request, HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
HttpSession session = request.getSession();
String ID =request.getParameter("ID");
String mypassword =request.getParameter("password");
String myType=request.getParameter("type");
session.setAttribute("ID", ID);
session.setAttribute("type", myType);
System.out.println(ID+" "+mypassword);
myDao cd = new myDao();
User list = cd.searchID(ID);
String rightPassword = list.getPassword();
System.out.println(rightPassword);
if(mypassword.equals(rightPassword)&&myType.equals(list.getType()))
{
request.setAttribute("message","密码正确");
if(myType.equals("1.员工"))
request.getRequestDispatcher("person.jsp").forward(request,response);
else if(myType.equals("2.教师"))
request.getRequestDispatcher("tercher.jsp").forward(request,response);
else
request.getRequestDispatcher("manager.jsp").forward(request,response);
}
else
{
request.setAttribute("message","密码错误或身份错误");
request.getRequestDispatcher("login.jsp").forward(request,response);
}
}
private void add_project(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String project=request.getParameter("project");
System.out.println("添加的课程为"+project);
myDao cd=new myDao();
if(cd.add_project(project))
{
request.setAttribute("message", "添加成功");
request.getRequestDispatcher("manager.jsp").forward(request, response);
}
else
{
request.setAttribute("message", "添加失败");
request.getRequestDispatcher("addProject.jsp").forward(request, response);
}
}
private void change_project(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String oldname=request.getParameter("oldname");
String newname=request.getParameter("newname");
System.out.println("修改课程"+oldname+"为"+newname);
myDao cd=new myDao();
if(cd.change_project(oldname,newname))
{
request.setAttribute("message", "修改成功");
request.getRequestDispatcher("manager.jsp").forward(request, response);
}
else
{
request.setAttribute("message", "修改失败");
request.getRequestDispatcher("change_project.jsp").forward(request, response);
}
}
private void delete_project(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String project=request.getParameter("project");
System.out.println("删除课程"+project);
myDao cd=new myDao();
if(cd.delete_project(project))
{
request.setAttribute("message", "删除成功");
request.getRequestDispatcher("manager.jsp").forward(request, response);
}
else
{
request.setAttribute("message", "删除失败");
request.getRequestDispatcher("change_project.jsp").forward(request, response);
}
}
private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String ID=request.getParameter("ID");
System.out.println("删除ID"+ID);
myDao cd=new myDao();
if(cd.delete(ID))
{
request.setAttribute("message", "删除成功");
request.getRequestDispatcher("list.jsp").forward(request, response);
}
else
{
request.setAttribute("message", "删除失败");
request.getRequestDispatcher("list.jsp").forward(request, response);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
HttpSession session = request.getSession();
String method =request.getParameter("method");
if(method.equals("manager_login"))
{
manager_longin(request, response);
}
else if(method.equals("login"))
{
try {
searchID(request,response);
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
else if(method.equals("update"))
{
try {
update(request,response);
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
else if(method.equals("list"))
{
try {
list(request,response);
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
else if(method.equals("searchMyInformation"))
{
try {
searchMyInformation(request,response);
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
else if(method.equals("back"))
{
try {
back(request,response);
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
else if(method.equals("delete"))
{
try {
delete(request,response);
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
else if(method.equals("add_project"))
{
try {
add_project(request,response);
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
}
}