完整的源代碼html
/** * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved. * * You may not modify, use, reproduce, or distribute this software except in * compliance with the terms of the License at: * https://github.com/javaee/tutorial-examples/LICENSE.txt */ package javaeetutorial.hello2; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * This is a simple example of an HTTP Servlet. It responds to the GET method of * the HTTP protocol. */ @WebServlet("/greeting") public class GreetingServlet extends HttpServlet { @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"); if (dispatcher != null) { dispatcher.include(request, response); } } out.println("</body></html>"); } } @Override public String getServletInfo() { return "The Hello servlet says hello."; } }
分析
String userName=request.getParameter(「userName」):定義了一個叫userName的字符串變量,並給他賦值。request.getParameter(「userName」);這句的意思是從request中拿出名字叫userName的值,賦給了你剛纔定義的變量。request是負責頁面之間傳遞參數和數據的類,是java封裝好的,咱們只要把它當成一個工具就能夠了。而後判斷username是否爲空或者長度爲0,若是爲空或者長度爲0就實現Servlet之間的請求傳遞(頁面跳轉),最後輸出。java