确实发现只有考核前学的东西才是最多的,感觉这两天学的用的都能顶上上面好几天的了。然后就是写这个项目的基础功能,还没有对用户的状态进行一个判断,写了两个情况,一个是管理员的,一个是用户的。
删除用户信息的代码块:
package com.example.servlet;
import com.example.service.UserService;
import com.example.service.impl.UserServiceImpl;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
@WebServlet("/delete")
public class DeleteServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) {
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
try {
request.setCharacterEncoding("utf-8");
int id = Integer.parseInt(request.getParameter("uid"));
UserService userService = new UserServiceImpl();
Boolean delete = userService.delete(id);
try {
PrintWriter out = response.getWriter();
if (delete) {
out.println("删除成功");
} else {
out.println("删除失败,此id不存在");
out.println("<button>" +
"<a href=\"http://localhost:8080/nihao/index.jsp\">点击返回主页面</a>" +
"</button>");
}
} catch (IOException e) {
throw new RuntimeException(e);
}
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
}
package com.example.servlet;
import com.example.service.UserService;
import com.example.service.impl.UserServiceImpl;
import com.example.util.JDBCUtil;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
@WebServlet("/add")
public class AddServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse resp) throws UnsupportedEncodingException {
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
String usname = request.getParameter("usename");
String uspass = request.getParameter("usepwd");
UserService userService = new UserServiceImpl();
Boolean register = userService.register(usname, uspass);
try {
PrintWriter out = resp.getWriter();
if (register) {
out.println("注册成功");
} else {
out.println("注册失败,该账号已存在。");
out.println("<button>" +
"<a href=\"http://localhost:8080/nihao/index.jsp\">点击返回主页面</a>" +
"</button>");
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
上面的是添加用户信息,即注册的代码块。
明天的考核一点把握都没有,还有功能没有写完,学长也说感觉我写的很乱。