JAVA SERVLET. Server-side Programming PROGRAMMING

Similar documents
Unit 4 - Servlet. Servlet. Advantage of Servlet

Unit-4: Servlet Sessions:

Advanced Internet Technology Lab # 6

Get the cookies from the service request: Cookie[] HttpServletRequest.getCookies() Add a cookie to the service response:

Handling Cookies. Agenda

Servlets and JSP (Java Server Pages)

Session 20 Data Sharing Session 20 Data Sharing & Cookies

Handout 31 Web Design & Development

Enterprise Java Unit 1- Chapter 4 Prof. Sujata Rizal Servlet API and Lifecycle

SERVLETS INTERVIEW QUESTIONS

Session 10. Form Dataset. Lecture Objectives

Handling Cookies. For live Java EE training, please see training courses at

Advanced Internet Technology Lab # 5 Handling Client Requests

Java4570: Session Tracking using Cookies *

Java servlets CSCI 470: Web Science Keith Vertanen Copyright 2013

To follow the Deitel publishing program, sign-up now for the DEITEL BUZZ ON-

Java Enterprise Edition. Java EE Oct Dec 2016 EFREI/M1 Jacques André Augustin Page 1

Module 4: SERVLET and JSP

Session 9. Data Sharing & Cookies. Reading & Reference. Reading. Reference http state management. Session 9 Data Sharing

INTRODUCTION TO SERVLETS AND WEB CONTAINERS. Actions in Accord with All the Laws of Nature

Servlets. An extension of a web server runs inside a servlet container

Servlets.

Component Based Software Engineering

Servlet. Web Server. Servlets are modules of Java code that run in web server. Internet Explorer. Servlet. Fire Fox. Servlet.

Unit III- Server Side Technologies

JAVA SERVLET. Server-side Programming ADVANCED FEATURES

JdbcResultSet.java. import java.sql.*;

Session 8. JavaBeans. Reading & Reference. Reading. Reference. Session 8 Java Beans. 2/27/2013 Robert Kelly, Head First Chapter 3 (MVC)

CE212 Web Application Programming Part 3

1. Introduction. 2. Life Cycle Why JSP is preferred over Servlets? 2.1. Translation. Java Server Pages (JSP) THETOPPERSWAY.

AJP. CHAPTER 5: SERVLET -20 marks

Kamnoetvidya Science Academy. Object Oriented Programming using Java. Ferdin Joe John Joseph. Java Session

Database Applications Recitation 6. Project 3: CMUQFlix CMUQ s Movies Recommendation System

INTERNET PROGRAMMING TEST-3 SCHEME OF EVALUATION 1.A 3 LIFE CYCLE METHODS - 3M 1.B HTML FORM CREATION - 2 M

WHITE LABELING IN PROGRESS ROLLBASE PRIVATE CLOUD

Form Data trong Servlet

Introduction. This course Software Architecture with Java will discuss the following topics:

Servlet Fudamentals. Celsina Bignoli

Using Java servlets to generate dynamic WAP content

JavaServer Pages (JSP)

Introduction. Literature: Steelman & Murach, Murach s Java Servlets and JSP. Mike Murach & Associates Inc, 2003

Advanced Web Technology

In servlet, form parsing is handled automatically. You call request.getparameter to get the value of a form parameter.

Server Side Internet Programming

CIS 3952 [Part 2] Java Servlets and JSP tutorial

Chapter 17. Web-Application Development

Advanced Internet Technology Lab # 4 Servlets

Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 2

Backend. (Very) Simple server examples

Chapter 10 Servlets and Java Server Pages

JAVA SERVLET. Server-side Programming INTRODUCTION

Programming on the Web(CSC309F) Tutorial 9: JavaServlets && JDBC TA:Wael Abouelsaadat

The Structure and Components of

Unit III- Server Side Technologies

Handling the Client Request: HTTP Request Headers

Lecture Notes On J2EE

Server and WebLogic Express

Advanced Topics in Operating Systems. Manual for Lab Practices. Enterprise JavaBeans

Servlets by Example. Joe Howse 7 June 2011

HTTP. HTTP HTML Parsing. Java

UNIT-VI. HttpServletResponse It extends the ServletResponse interface to provide HTTP-specific functionality in sending a response.

Principles and Techniques of DBMS 6 JSP & Servlet

Servlet 5.1 JDBC 5.2 JDBC

2. Follow the installation directions and install the server on ccc. 3. We will call the root of your installation as $TOMCAT_DIR

ICOM 5016 Database Systems. Database Users. User Interfaces and Tools. Chapter 8: Application Design and Development.

Servlets1. What are Servlets? Where are they? Their job. Servlet container. Only Http?

First Simple Interactive JSP example

Session 8. Introduction to Servlets. Semester Project

ServletConfig Interface

COMP9321 Web Application Engineering

Session 9. Introduction to Servlets. Lecture Objectives

Servlet And JSP. Mr. Nilesh Vishwasrao Patil, Government Polytechnic, Ahmednagar. Mr. Nilesh Vishwasrao Patil

Internet Technologies 6 - Servlets I. F. Ricci 2010/2011

HttpServlet ( Class ) -- we will extend this class to handle GET / PUT HTTP requests

Lab session Google Application Engine - GAE. Navid Nikaein

HTTP Request Handling

Servlet and JSP Review

Integrating Servlets and JavaServer Pages Lecture 13

Scheme G Sample Question Paper Unit Test 2

A Servlet-Based Search Engine. Introduction

SNS COLLEGE OF ENGINEERING, Coimbatore

EXPERIMENT- 9. Login.html

Chapter 29 Servlets: Bonus for Java Developers 1041

CREATE A SERVLET PROGRAM TO DISPLAY THE STUDENTS MARKS. To create a servlet program to display the students marks

Java E-Commerce Martin Cooke,

To create a view for students, staffs and courses in your departments using servlet/jsp.

CS 112 Introduction to Programming

Construction d Applications Réparties / Master MIAGE

Introduction. This course Software Architecture with Java will discuss the following topics:

Java Server Pages JSP

COMP9321 Web Application Engineering

Web Application Services Practice Session #2

Servlet Basics. Agenda

Web based Applications, Tomcat and Servlets - Lab 3 -

********************************************************************

JSP Scripting Elements

Generating the Server Response: HTTP Status Codes

Generating the Server Response: HTTP Response Headers

&' () - #-& -#-!& 2 - % (3" 3 !!! + #%!%,)& ! "# * +,

PSD1B Advance Java Programming Unit : I-V. PSD1B- Advance Java Programming

Transcription:

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