JAVA SERVLET Server-side Programming PROGRAMMING 1
AGENDA Passing Parameters Session Management Cookie Hidden Form URL Rewriting HttpSession 2
HTML FORMS Form data consists of name, value pairs Values are retrieved on the server by name GET passes data in the query string POST passes data in content of request 3
FORM CONTROLS input : many kinds of form data Text fields, checkboxes, radio buttons, passwords, buttons, hidden controls, file selectors, object controls button : type=submit button reset select : a menu, contains option child elements textarea : multi-line text input field Other html tags can be present (e.g. format forms in tables) 4
PASSING PARAMETERS <html> <body> <FORM ACTION="Parameters" METHOD="GET"> Enter User Name: <INPUT TYPE="TEXT" NAME="username"><BR> <INPUT TYPE="SUBMIT" VALUE="LOGIN"> </FORM> </body> </html> http://localhost:8080/lectureadvancedjavaweb/parameters? username=aaa 5
GET VS POST GET Exposes data through browser URL Browsers restrict the character size of query string to be 255 characters. POST Is more secured way of posting page data No size restrictions as such. 6
GET PARAMETERS public void doget(httpservletrequest request, HttpServletResponse response) throws ServletException, IOException { } response.setcontenttype("text/html"); PrintWriter out = response.getwriter(); String username, password; username = request.getparameter("username"); password = request.getparameter("password"); out.println("<html><body><b>user Name</B>: "); out.println(username); out.println("<b>password</b>: "); out.println(password); out.println("</body></html>"); 7
ALL PARAMETERS 8
ALL PARAMETERS getparameter(): You call request.getparameter() method to get the value of a form parameter. getparametervalues(): Call this method if the parameter appears more than once and returns multiple values, for example checkbox. getparameternames(): Call this method if you want a complete list of all parameters in the current request. 9
ALL PARAMETERS Enumeration<String> paramnames = request.getparameternames(); while (paramnames.hasmoreelements()) { String paramname = (String) paramnames.nextelement(); out.print("<tr><td>" + paramname + "</TD>\n<TD>"); String[] paramvalues = request.getparametervalues(paramname); if (paramvalues.length == 1) { String paramvalue = paramvalues[0]; if (paramvalue.length() == 0) { out.println("<i>no Value</I>"); } else { out.println(paramvalue); } } else { out.println("<ul>"); for (int i = 0; i < paramvalues.length; i++) { out.println("<li>" + paramvalues[i]); } out.println("</ul></td></tr>"); } } 10
SESSIONS Http protocol is a stateless means each request is considered as the new request. Session simply means a particular interval of time. Session Tracking is a way to maintain state (data) of an user. It is also known as session management. 11
SESSION TRACKING Cookies Hidden Form Field URL Rewriting HttpSession 12
COOKIES A small piece of information that is persisted between the multiple client requests. A cookie has: a name, a single value, and optional attributes such as a comment, path, a maximum age, and a version number. 13
COOKIES javax.servlet.http.cookie Constructors Cookie() Cookie(String name, String value) Useful Methods public void setmaxage(int expiry) public String getname() public String getvalue() public void setname(string name) public void setvalue(string value) 14
OTHER METHODS REQUIRED public void addcookie(cookie ck) method of HttpServletResponse interface is used to add cookie in response object. public Cookie[] getcookies() method of HttpServletRequest interface is used to return all the cookies from the browser. 15
STEPS 16
NEW OR OLD VISITOR? Welcome to the world of Cookie Welcome Back 17
REPEAT VISIOR boolean newbie = true; Cookie[] cookies = request.getcookies(); if (cookies!= null) { for (int i = 0; i < cookies.length; i++) { Cookie c = cookies[i]; if ((c.getname().equals("visitor")) && (c.getvalue().equals("yes"))) { } } } newbie = false; break; String title; if (newbie) { Cookie repeatvisitor = new Cookie("visitor", "yes"); repeatvisitor.setmaxage(60); response.addcookie(repeatvisitor); title = "Welcome to the world of Cookie"; } else { title = "Welcome Back"; } response.setcontenttype("text/html"); PrintWriter out = response.getwriter(); out.print("<html><body"); out.print("<h2>" + title + "</h2>"); out.print("</body></html>"); 18
HIDDEN FORM FIELD SRC: https://www.studytonight.com/servlet/hidden-form-field.php <input type="hidden" name="uname" value= abc"> String user = request.getparameter("uname"); out.print("<input type='hidden' name='uname' value='"+user+"'>"); 19
URL REWRITING Hello?id=123&user=abc out.print("<a href= Visitor?uname="+name+"'>visit</a>"); response.sendredirect( Visitor?uname="+name+"); 20
HTTPSESSION INTERFACE Sessions are represented by an HttpSession object. You access a session by calling the getsession method of a request object. This method returns the current session associated with this request; or, if the request does not have a session, this method creates one. 21 SRC: https://www.studytonight.com/servlet/session-management.php
STEPS 22
SOME IMPORTANT METHODS Object getattribute(string name) Void setattribute(string name, Object value) long getcreationtime() String getid() long getlastaccessedtime() int getmaxinactiveinterval() void invalidate() boolean isnew() void setmaxinactiveinterval(int interval) 23
COUNTING NUMBER OF VISITS 24
COUNTING NUMBER OF VISITS HttpSession session = request.getsession(); String heading; Integer accesscount = (Integer) session.getattribute("accesscount"); if (accesscount == null) { accesscount = new Integer(0); heading = "Welcome, Newcomer"; } else { heading = "Welcome Back"; accesscount = new Integer(accessCount.intValue() + 1); } session.setattribute("accesscount", accesscount); PrintWriter out = response.getwriter(); out.println("<html><body ><H1>" + heading + "</H1><BR>"); out.println("<h2>information on Your Session:</H2><BR>"); out.println("<table BORDER=1><TR BGCOLOR=\"#FFAD00\">"); out.println(" <TH>Info Type</TH><TH>Value<TH></TR>"); out.println(" <TR><TD>ID\n</TD><TD>" + session.getid() + "</TD></TR>"); out.println("<tr><td>creation Time</TD><TD>" + new Date(session.getCreationTime()) + "</TD></TR>"); out.println("<tr><td>time of Last Access</TD><TD>" + new Date(session.getLastAccessedTime()) + "</TD></TR>"); out.println("<tr><td>number of Previous Accesses</TD><TD>" + accesscount + "</TD></TR>"); out.println("</table></body></html>"); 25
REFERENCES https://docs.oracle.com/javaee/7/jeett.pdf 26
THANK YOU 27