每日日报2020 10/25
今天我写了servlet,有一说一,不太明白,但是感觉还是Java代码,能写。
package shiyan1;
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 shiyan1.Dao;
import shiyan1.Bean;
/**
* Servlet implementation class Servlet2
*/
@WebServlet("/Servlet2")
public class Servlet2 extends HttpServlet {
private static final long serialVersionUID = 1L;
static String wen;
/**
* @see HttpServlet#HttpServlet()
*/
public Servlet2() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
/*
response.getWriter().append("Served at: ").append(request.getContextPath());
request.setCharacterEncoding("utf-8");
String id = request.getParameter("id");
String name = request.getParameter("name");
String aclass =request.getParameter("aclass");
String kecheng =request.getParameter("kecheng");
String score =request.getParameter("score");
Bean bean = new Bean(id,name,aclass,kecheng,score);
Dao dao = new Dao();
boolean f = dao.add(bean);
if(f){
request.setAttribute("message", "注册成功");
request.getRequestDispatcher("test.jsp").forward(request,response);
}
else {
request.setAttribute("message", "已有帐号,重复登陆");
request.getRequestDispatcher("users.jsp").forward(request,response);
}
*/
Dao dao=new Dao();
request.setCharacterEncoding("utf-8");
String id =request.getParameter("id");
boolean wen=dao.delete(id);
if(wen)
{
request.setAttribute("message", "删除成功!");
request.getRequestDispatcher("test.jsp").forward(request,response);
}
else
{
request.setAttribute("message", "此用户不存在,删除失败");
request.getRequestDispatcher("delete.jsp").forward(request,response);
}
}
@SuppressWarnings("unused")
private void look(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
{
Dao dao =new Dao();
req.setCharacterEncoding("utf-8");
String id = req.getParameter("id");
wen=id;
String q1=dao.find(id);
if(q1.equals("@"))
{
req.setAttribute("message", "此用户不存在,不能进行修改");
req.getRequestDispatcher("update.jsp").forward(req,resp);
return;
}
Bean user3 = dao.getUserbyid(id);
req.setAttribute("user3", user3);
req.getRequestDispatcher("update2.jsp").forward(req,resp);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}