27
JAVA SERVLET Server-side Programming PROGRAMMING 1

Java Servlet - syedimtiyazhasan.files.wordpress.com · 3/7/2018 · HTML FORMS Form data consists of name, value pairs Values are retrieved on the server by name GET passes data in

  • Upload
    vokiet

  • View
    220

  • Download
    0

Embed Size (px)

Citation preview

JAVA SERVLETServer-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>

5http://localhost:8080/LectureAdvancedJavaWeb/Parameters?

username=aaa

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, HttpServletResponseresponse) 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;

}

}

}

18

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>");

HIDDEN FORM FIELD

<input type="hidden" name="uname" value=“abc">

String user = request.getParameter("uname");

out.print("<input type='hidden' name='uname' value='"+user+"'>"); 19

SRC: https://www.studytonight.com/servlet/hidden-form-field.php

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