Servlets

  1. HashMaps
    beans
    cast strings
    parse ints
    set object attributes
        getters and setters
    .equals(some string)
  2. how to send form element data to a servlet
    • ^form action = "LoginServlet" id="formId">
    • Username
    • ^input type="text" name="username" value ="admin">
    • ^br>
    • ^input type="submit" value="Submit">
    • ^/form>

    • ^form action = 'AddServlet'>
    • ^label for='num1'>Number 1 </label>
    • input id='num1' name='num1' value='12'/>
    • ^input type='submit' value='add'/>
    • ^/form>
  3. The entry page of a website is "InitServlet" and the navigates to  "/jsp/login.jsp".
    What the important parts?
    • @WebServlet(name = "InitServlet", urlPatterns = {"/InitServlet"})
    • public class InitServlet extends HttpServlet {
    •     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    •             throws ServletException, IOException {
    •         response.setContentType("text/html;charset=UTF-8");
    •        //more code here
    •         request.getRequestDispatcher("/jsp/login.jsp").forward(request, response);
    •     }
    • }
  4. What are important imports for a servlet
    • import javax.servlet.ServletException;
    • import javax.servlet.annotation.WebServlet;
    • import javax.servlet.http.HttpServlet;
    • import javax.servlet.http.HttpServletRequest;
    • import javax.servlet.http.HttpServletResponse;
  5. How do you capture the following?
    ^form action = "LoginServlet" id="formId">
    Username
    ^input type="text" name="username" value ="admin">
    ^br>
    ^input type="submit" value="Submit">
    ^/form>
    String username = (String) request.getParameter("username");
  6. login.jsp uses UserClass and HashMap. What are important parts of the .jsp?
    • ^%@page import="model.UserClass"%>
    • ^%@page import="java.util.HashMap;"%>
    • ^%@page contentType="text/html" pageEncoding="UTF-8"%>
  7. How do you navigate from a servlet to "/jsp/loginFail.jsp"
    request.getRequestDispatcher("/jsp/loginFail.jsp").forward(request, response);
  8. important parts of AddServlet
    • @WebServlet(name = "AddServlet", urlPatterns = {"/AddServlet"})
    • public class AddServlet extends HttpServlet
    • {
    • protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    •      throws ServletException, IOException
    •    {
    •       response.setContentType(
    •            "text/html;charset=UTF-8");
    •       //add code
    •    } //close servlet
    • } //close class
  9. How do you assign userObj to "sessionUserObj" to have session scope?
    UserClass userObj = userDir.get(username);
    request.getSession().setAttribute("sessionUserObj", userObj);
  10. sessionUserObj has session scope and has a property called username. How is the username accessed in a jsp?
    ^p>Username: ${sessionUserObj.getUsername()}</p>
  11. "user" has session scope. How to you delete it?
    request.getSession().removeAttribute("user");
  12. How is userObj accessed in another servlet?
    UserClass userObj = userDir.get(username);
    request.getSession().setAttribute("sessionUserObj", userObj);
    UserClass sessionUserObj = (UserClass)request.getSession().getAttribute("sessionUserObj");
  13. How do you assign userDirObj to "userDir" to have application scope?
    HashMap<String, UserClass> userDirObj = new HashMap();
    getServletContext().setAttribute("userDir", userDirObj );
  14. How is userDir accessed in another servlet?
    HashMap<String, UserClass> userDir = new HashMap();
    getServletContext().setAttribute("userDir", userDir);
    HashMap<String, UserClass> userDir = (HashMap<String, UserClass>) getServletContext().getAttribute("userDir");
  15. number1 and number2 are elements of a form. Send the sum as a request attribute and product as a session attribute to results.jsp.
    • int num1 = Integer.parseInt(request.getParameter("number1"));
    • int num2 = Integer.parseInt(request.getParameter("number2"));
    • Integer sum= num1 + num2;
    • Integer product = num1 * num2;
    • request.setAttribute("sum", sum);
    • request.getSession().setAttribute("product", product);
    • request.getRequestDispatcher("result.jsp").forward(request, response);
  16. "result" is a request attribute and
    "product" is a session attribute sent from a servlet.
    How do you capture and display the values in a jsp?
    • ^body>
    • ^% Integer total = (Integer)request.getAttribute("result");    
    • out.println("^h1>Scriptlet Result = " + total.toString() + "^/h1>");
    • %>

    ^h1>Request Attribute Result = ${result} ^/h1>

    • ^h1>Session Attribute Result = ${product} ^/h1>
    • ^/body>
  17. How do you declare a hashmap of String and  UserClass called userDir
    HashMap<String, UserClass> userDir = new HashMap();
  18. mike has the attributes "mike", "mike", "admin", "mike@comp31.com.
    How do you add mikeObj to the hashmap shows?
    HashMap<String, UserClass> userDir;
    userDir.put("mike", new UserClass("mike", "mike", "admin", "mike@comp31.com"));
  19. How do you set the object userDir to have application scope?
    getServletContext().setAttribute("userDir", userDir);
  20. login.jsp is in the jsp folder. How do you navigate to login.jsp from a servlet?
    request.getRequestDispatcher("/jsp/login.jsp").forward(request, response);
  21. What is a JavaBean?
    • is a specially constructed Java class coded according to the JavaBeans API specifications
    • distinguish a JavaBean from other Java classes −
    • It provides a default, no-argument constructor.
    • It should be serializable and that which can implement the Serializable interface.
  22. set a request bean of class DemoBean in a servlet
    • DemoBean bean = new DemoBean();
    • bean.setMsg("This is a Request Bean");
    • request.setAttribute("bean1", bean);
  23. set a session bean of class DemoBean in a servlet
    • DemoBean sessionBean = new DemoBean();
    • sessionBean.setMsg("This is a Session bean");
    • request.getSession().setAttribute("bean2", sessionBean);
  24. set an application bean of class DemoBean in a servlet
    • DemoBean appBean = new DemoBean();
    • appBean.setMsg("This is an App bean");
    • getServletContext().setAttribute("bean3", appBean);
  25. set a bean with page scope in jsp
    • ^jsp:useBean id="pagebean" scope="page" class="model.DemoBean">
    • ^jsp:setProperty name="pagebean" property="msg" value="Message from page Bean"/>
    • ^/jsp:useBean>
  26. set a bean with application scope in a jsp
    • ^jsp:useBean id="appbean" scope="application" class="model.DemoBeam">
    • ^jsp:setProperty name="appbean" property="msg" value="Message from app Bean"/>
    • ^/jsp:useBean?
  27. What is this?
    ^%
            pageContext.setAttribute("PageName", "JavaBeans and Scope");
        %>
    ^h2>${PageName}</h2>
  28. how can a page take user input?
    • pass parameters via the url
    • at end of url add ?parameterName=something
    • (no quotes needed)
    • or
    • user input through textboxes
  29. How does a servlet handle a parameter?
    • use HttpServletRequest object(request)
    • String var = request.getParameter("parameterName");
  30. how to add beans
    • package model;
    • import java.io.Serializable;
    • public class DemoBean extends Parent implements Serializable{
    •     private String msg;
    •     public DemoBean() {
    •         msg = "Hi";
    •     }
    •     public String getMsg() {
    •         return msg;
    •     }
    •     public void setMsg(String msg) {
    •         this.msg = msg;
    •     }
    •     @Override
    •     public String toString() {
    •         return "DemoBean: " + "msg=" + msg;
    •     }
    • }
  31. How do you add demoBean of type DemoBeam to an array list called beans?
    • ArrayList<DemoBean> beans = new ArrayList<>();
    • DemoBean demoBean = new DemoBean();
    • demoBean.setMsg("This is the first item");
    • beans.add(demoBean);
Author
slc53
ID
335590
Card Set
Servlets
Description
Servlets
Updated