小明滚出---响应对象HttpServletResponse和请求对象HttpServletRequest实例

时间:2022-01-28 14:18:39

《Servlet类的Java》

@WebServlet("/studentServlet")

public class StudentServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @doGet方法
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}

/**
* @doPost方法
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1:设置请求的字符集
request.setCharacterEncoding("UTF-8");
//2:设置回应的文本类型
response.setContentType("text/html;charset=UTF-8");
//3:获取客户输入的姓名
String name = request.getParameter("stuname");
//4:获取输出流
PrintWriter out = response.getWriter();
//4:判断
if(name == null|| "".equals(name)){
response.sendRedirect("playXiaoming.html");
}else if("小明".equals(name)){
//向servlet页面写入
out.write("<font color=red size = 30px>小明滚出去</font>");
}else{
//向servlet页面写入
out.write("<font color=#83e5fa size = 30px>"+name+",欢迎你!!!</font>");
}
//刷新关闭流
out.flush();
out.close();
}

}