public void service(HttpServletRequest req, HttpServletResponse res) {
   try {
     res.setContentType("text/html");
     PrintWriter out = res.getWriter();
     out.println("<html><body>");
     ServletContext ctx = getServletContext();
     String name = (String) ctx.getAttribute("con");
     out.println("name= " + name);
     out.println("</html></body>");
   } catch (Exception e) {
   }
 }
 @Override
 public void doGet(HttpServletRequest request, HttpServletResponse response)
     throws ServletException, IOException {
   response.setContentType("text/html");
   PrintWriter out = response.getWriter();
   String title = "Test Servlet with Utilities";
   out.println(
       ServletUtilities.headWithTitle(title)
           + "<body bgcolor=\"#fdf5e6\">\n"
           + "<h1>"
           + title
           + "</h1>\n"
           + "<p>Simple servlet for testing.</p>\n"
           + "</body></html>");
 }
示例#3
0
 public void doGet(HttpServletRequest request, HttpServletResponse response)
     throws ServletException, IOException {
   response.setContentType("text/html"); // must come first
   PrintWriter out = response.getWriter();
   out.println(
       "<html><head><title>Hello Client!</title></head><body>"
           + "<h1>Hello Client!</h1>"
           + "</body></html>");
 }
  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/html");
    HttpSession session = request.getSession();

    String name = request.getParameter("name");
    String ID = request.getParameter("ID");
    String email = request.getParameter("email");
    String password = request.getParameter("password");
    String position = request.getParameter("position");

    try {
      MD5Util.addNewStaff(name, Integer.parseInt(ID), email, password, position);
      session.setAttribute("AddStaff", "Yes");
    } catch (Exception e) {
      session.setAttribute("AddStaff", "No");
    }

    response.sendRedirect("/library/people.jsp");
  }
示例#5
0
  @Override
  public void doGet(HttpServletRequest req, HttpServletResponse res)
      throws ServletException, IOException {

    String keywords = (req.getParameter("search")).replace(" ", "%20");
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();
    String docType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " + "Transitional//EN\">\n";
    String myAppId = "MarkWill-00a1-4cb0-b23d-5d85d0c939f1";

    out.println(
        "<html>\n"
            + "<body>\n"
            + "The eBay response should be here: "
            + "<a href=http://open.api.ebay.com/shopping?callname=FindHalfItems&appid="
            + myAppId
            + "&version=527&QueryKeywords="
            + keywords
            + "&responseencoding=SOAP>Here is an XML response from eBay"
            + "</a>\n"
            + "</body>\n"
            + "</html>\n");
  }
  /**
   * Handles HTTP POST requests.
   *
   * @param request Description of the Parameter
   * @param response Description of the Parameter
   * @exception ServletException if there is a Servlet failure
   * @exception IOException if there is an IO failure
   */
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    response.setContentType("text/html");
    HttpSession session = request.getSession();

    System.out.println(
        "In Project4AddServlet...get form parms of Employee, set into INSERT sql, return to add.");
    String url = null;

    String firstName = request.getParameter("firstName");
    String lastName = request.getParameter("lastName");
    String ssn = request.getParameter("ssn");
    String department = request.getParameter("department");
    String roomNumber = request.getParameter("roomNumber");
    String phoneNumber = request.getParameter("phoneNumber");

    // These session properties will persist between validations, in the JSP, input tag, text
    // value="${EL item}".
    session.setAttribute("firstName", firstName);
    session.setAttribute("lastName", lastName);
    session.setAttribute("ssn", ssn);
    session.setAttribute("department", department);
    session.setAttribute("roomNumber", roomNumber);
    session.setAttribute("phoneNumber", phoneNumber);

    EmployeeDirectory employeeDirectory =
        (EmployeeDirectory) getServletContext().getAttribute("employeeDirectory");

    // associate the Message with the request, and clear it before forwarding to JSP page
    String AddMessage = "";
    boolean firstNameErr = false;
    boolean lastNameErr = false;
    boolean ssnErr = false;
    boolean departmentErr = false;
    boolean roomNumberErr = false;
    boolean phoneNumberErr = false;

    // Validate that all fields have valid data, prior to employeeDirectory.Add()

    if (firstName == null || firstName.equals("")) {
      AddMessage = "Please enter missing First Name.";
      firstNameErr = true;
    } else if (lastName == null || lastName.equals("")) {
      AddMessage = "Please enter missing Last Name.";
      lastNameErr = true;
    } else if (ssn == null || ssn.equals("")) {
      AddMessage = "Please enter missing SSN.";
      ssnErr = true;
    } else if (department == null || department.equals("")) {
      AddMessage = "Please enter missing Department.";
      departmentErr = true;
    } else if (roomNumber == null || roomNumber.equals("")) {
      AddMessage = "Please enter missing Room Number.";
      roomNumberErr = true;
    } else if (phoneNumber == null || phoneNumber.equals("")) {
      AddMessage = "Please enter missing Phone Number.";
      phoneNumberErr = true;
    } else {
      employeeDirectory.addNewEmployee(
          firstName, lastName, ssn, department, roomNumber, phoneNumber);
      AddMessage = "Employee added.";
    }

    session.setAttribute("project4AddMessage", AddMessage);
    session.setAttribute("project4firstNameErr", firstNameErr);
    session.setAttribute("project4lastNameErr", lastNameErr);
    session.setAttribute("project4ssnErr", ssnErr);
    session.setAttribute("project4departmentErr", departmentErr);
    session.setAttribute("project4roomNumberErr", roomNumberErr);
    session.setAttribute("project4phoneNumberErr", phoneNumberErr);

    url = "/java112/Project4EmployeeAdd.jsp"; // do a redirect using url back to
    // Project4EmployeeAdd.jsp page.
    response.sendRedirect(url);
  }