首页 > 其他 > 详细

hello2分析

时间:2019-03-25 10:10:41      阅读:145      评论:0      收藏:0      [点我收藏+]

public class GreetingServlet extends HttpServlet {

//使用doget方法,传入request和response两个参数,返回text/html且最大字节数为8192

@Override
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
response.setBufferSize(8192);

//向客户端浏览器页面输入前端数据
try (PrintWriter out = response.getWriter()) {
out.println("<html lang=\"en\">"
+ "<head><title>Servlet Hello</title></head>");

// then write the data of the response
out.println("<body bgcolor=\"#ffffff\">"
+ "<img src=\"resources/images/duke.waving.gif\" "
+ "alt=\"Duke waving his hand\">"
+ "<form method=\"get\">"
+ "<h2>Hello, my name is Duke. What‘s yours?</h2>"
+ "<input title=\"My name is: \" type=\"text\" "
+ "name=\"username\" size=\"25\"/>"
+ "<p></p>"
+ "<input type=\"submit\" value=\"Submit\"/>"
+ "<input type=\"reset\" value=\"Reset\"/>"
+ "</form>");

String username = request.getParameter("username");
if (username != null && username.length() > 0) {
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher("/response");//若username不为空,则使用dispatcher接收客户端请求并发送到服务器

if (dispatcher != null) {
dispatcher.include(request, response);//若客户端请求不为空,则响应资源
}
}
out.println("</body></html>");
}
}

@Override
public String getServletInfo() {
return "The Hello servlet says hello.";

}
}

 

@WebServlet("/response")
public class ResponseServlet extends HttpServlet {

@Override
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
try (PrintWriter out = response.getWriter()) {

//对用户输入的信息进行判别,并通过responseServlet进行处理
String username = request.getParameter("username");
if (username != null && username.length() > 0) {
out.println("<h2>Hello, " + username + "!</h2>");
}
}
}

@Override
public String getServletInfo() {
return "The Response servlet says hello.";

hello2分析

原文:https://www.cnblogs.com/chhhh/p/10591798.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!