You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
text/src/web/servlet/login/FindStudentServlet.java

59 lines
2.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package web.servlet.login;
import com.fasterxml.jackson.databind.ObjectMapper;
import domain.Student;
import service.StudentService;
import service.impl.StudentServiceImpl;
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 java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* 查找学生信息的Servlet
*/
@WebServlet("/findStudentServlet")
public class FindStudentServlet extends HttpServlet {
/**
* 处理POST请求根据学生ID查找学生信息。
*
* @param request HTTP请求对象包含客户端发送的数据。
* @param response HTTP响应对象用于生成返回给客户端的响应。
* @throws ServletException 如果处理请求时发生错误。
* @throws IOException 如果输入或输出异常发生。
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json;charset=utf-8");
String studentId = request.getParameter("studentid");
Student student = new Student();
Student findStudent = new Student();
student.setS_id(studentId);
StudentService service = new StudentServiceImpl();
findStudent = service.findStudentById(student);
Map<String,Object>map = new HashMap<String,Object>();
try {
if (studentId.equals(findStudent.getS_id())) {
map.put("studentExsit", true);
map.put("msg", "ID已存在");
} else {
map.put("studentExsit", false);
map.put("msg", "用户名可用");
}
//map转为json传给客户端-
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(response.getWriter(),map);
} catch (Exception e) {
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}