⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sessionservlet.java

📁 21天学通java的示例程序源代码
💻 JAVA
字号:
import javax.servlet.*;import javax.servlet.http.*;import java.io.*;import java.util.*;/** * This servlet demonstrates how to manipulate and use sessions. */public class SessionServlet extends HttpServlet {  /**   * Process the HTTP Get request.   */  public void doGet(HttpServletRequest request,                     HttpServletResponse response) throws ServletException,                     IOException {    HttpSession session = request.getSession();    if (session.isNew()) {      // This is a new session, ask the user for his data      askForIdentification(request, response);    } else {      // This session is not new,      // respond with the data associated with the session      clientAlreadyKnown(request, response);    }   }   /**   * Send a form to the client asking for the user's name and favorite color.   */  private void askForIdentification(HttpServletRequest request,                                     HttpServletResponse response) throws IOException {    response.setContentType("text/html");    PrintWriter out = response.getWriter();    out.println("<html>");    out.println("<head><title>SessionServlet</title></head>");    out.println("<body>");    // Print a form asking for a name and a favorite color.    // Notice the use of encodeURL() to make sure the session tracking works    String my_url = request.getScheme() + "://" + request.getServerName()                     + ":" + request.getServerPort()                     + request.getContextPath() + request.getServletPath();    out.println("<FORM METHOD=\"post\" ACTION=\""                 + response.encodeURL(my_url) + "\">");    out.println("<br>");    out.println("What is your name: <INPUT TYPE=\"text\" NAME=\"name\" >");    out.println("<br>");    out      .println("What is your favorite color: <INPUT TYPE=\"text\" NAME=\"color\" >");    out.println("<br>");    out.println("<INPUT TYPE=\"submit\" >");    out.println("<br>");    out.println("</FORM>");    out.println("</body></html>");  }   /**   * Handle the case where we already know the user's name   * and favorite color. Respond with the name and color.   */  private void clientAlreadyKnown(HttpServletRequest request, HttpServletResponse response)           throws javax.servlet.ServletException, java.io.IOException {    // inspect the session to get the client's name and favorite color    HttpSession session = request.getSession();    String name = (String) session.getAttribute("name");    String color = (String) session.getAttribute("color");    response.setContentType("text/html");    PrintWriter out = response.getWriter();    out.println("<html>");    out.println("<head><title>SessionServlet</title></head>");    out.println("<body>");    out.println("I know you, " + name + ", your favorite color is "                 + color);    out.println("</body></html>");  }   /**   * Process the HTTP POST request.   * Extract the form data from the request and store it   * as session attributes for later use.   */  protected void doPost(HttpServletRequest request, HttpServletResponse response)           throws javax.servlet.ServletException, java.io.IOException {    // have to get the session before writing any output    HttpSession session = request.getSession();    response.setContentType("text/html");    PrintWriter out = response.getWriter();    out.println("<html>");    out.println("<head><title>SessonServlet</title></head>");    out.println("<body>");    // Get the user's name and favorite color from the form    String name = request.getParameter("name");    String color = request.getParameter("color");    // Set two attributes in the session to hold them.    session.setAttribute("name", name);    session.setAttribute("color", color);    out.println("Thanks " + name + ", I like " + color + " too!<br>");    out.println("</body></html>");  } }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -