Srujan Jsp

  • Uploaded by: satyanarayana
  • 0
  • 0
  • December 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Srujan Jsp as PDF for free.

More details

  • Words: 2,270
  • Pages: 38
JSP

1

How JavaServer Pages work • • • •

Use a Web server that supports JSP File extension *.jsp Embed script in HTML code No compiling, no packages, no CLASSPATH • Web server writes a servlet, with the static HTML simply being printed to the output stream associated with the servlet's service method. 2

JSP Scripting Elements • Expressions of the form <%= code %> Evaluated and inserted into the servlet's output • Scriptlets of the form <% expression %> Inserted into the servlet's _jspService method (called by service) • Declarations of the form Inserted into the body servletclass

3

Directive • of the form <@ directive attribute="value" %> • page – Case-sensitive attributes: import, contentType, isThreadSafe, session, buffer, autoflush, extends, info, errorPage, isErrorPage, and language

4

Directive • include – Lets you insert a file into the servlet class at the time the JSP file is translated into a servlet Include <jsp:include page =”” /> – This inclusion of file is dynamic and the specified file is included in the JSP file at runtime i.e. out put of the included file is inserted into the JSP file. Forward <jsp:forward page=”” /> – This will redirect to the different page without notifying browser. And many more. 5

Custom Tags taglib – Used to define custom mark-up tags

Syntax: <%@ taglib uri=”” prefix=”” %> Attributes: a. uri = “” b. prefix = “” prefix is alias name for the tag library name. 6

Other Servers that support JSP • Apache Tomcat. Tomcat is the official reference implementation of the servlet 2.2 and JSP 1.1 specifications. It can be used as a small stand-alone server for testing servlets and JSP pages, or can be integrated into the Apache Web server. 7

Allaire JRun. • JRun is a servlet and JSP engine that can be plugged into Netscape Enterprise or FastTrack servers, IIS, Microsoft Personal Web Server, older versions of Apache

8

New Atlanta’s ServletExec • ServletExec is a fast servlet and JSP engine that can be plugged into most popular Web servers for Solaris, Windows, MacOS, HPUX and Linux. You can download and use it for free, but many of the advanced features and administration utilities are disabled until you purchase a license. 9

• Gefion's LiteWebServer (LWS). LWS is a small free Web server that supports servlets version 2.2 and JSP 1.1. • GNU JSP. free, open source engine that can be installed on apache web server. • PolyJsp is based on XML/XSL and has been designed to be extensible. • JRUN. Available for IIS server. • WebSphere IBM's WebSphere very large application server now implements JSP. 10

Developing first JSP • Java Server Pages are save with .jsp extension. Following code which generates a simple html page. First JSP page.

<%="Java Developers Paradise"%>

<%="Hello JSP"%>

In jsp java codes are written between '<%' and '%>' tags. So it takes the following form : <%= Some Expression %> In this example we have use 11 <%="Java Developers Paradise"%>

JSP ARCHITECTURE • JSP files are finally compiled into a servlet by the JSP engine. Compiled servlet is used by the engine to serve the requests. • javax.servlet.jsp package defines two classes: JSPPage HttpJspPage • These classes defines the interface for the compiled JSP page. These interfaces are: jspInit() jspDestroy() _jspService(HttpServletRequest request,HttpServletResponse response) 12

INTRODUCTION TO JSP TAGS • Directives In the directives we can import packages, define error handling pages or the session information of the JSP page. • Declarations This tag is used for defining the functions and variables to be used in the JSP. • Scriplets In this tag we can insert any amount of valid java code and these codes are placed in _jspService method by the JSP engine. • Expressions We can use this tag to output any data on the generated page. These data are automatically converted to string and printed on the output stream. 13

DIRECTIVES • Syntax of JSP directives is: <%@directive attribute="value" %> Where directive may be: • page: page is used to provide the information about it. Example: <%@page language="java" %> • include: include is used to include a file in the JSP page. Example: <%@ include file="/header.jsp" %> • taglib: taglib is used to use the custom tags in the JSP pages (custom tags allows us to defined our own tags). Example: • <%@ taglib uri="tlds/taglib.tld" prefix="mytag" %> 14

attribute may be: •

language="java" This tells the server that the page is using the java language. Current JSP specification supports only java language. Example: <%@page language="java" %>



extends="mypackage.myclass" This attribute is used when we want to extend any class. We can use comma(,) to import more than one packages. Example: <%@page language="java" import="java.sql.*,mypackage.myclass" %>



session="true" When this value is true session data is available to the JSP page otherwise not. By default this value is true. Example: <%@page language="java" session="true" %>



errorPage="error.jsp" errorPage is used to handle the un-handled exceptions in the page. Example: <%@page language="java" session="true" errorPage="error.jsp" %>



contentType="text/html;charset=ISO-8859-1" Use this attribute to set the mime type and character set of the JSP. Example: <%@page language="java" session="true" contentType="text/html;charset=ISO-8859-1" %> 15

INTRODUCTION TO JSP DECLARATIVES

• Syntax of JSP Declaratives are: <%! //java codes %> • JSP Declaratives begins with <%! and ends %> with .We can embed any amount of java code in the JSP Declaratives. Variables and functions defined in the declaratives are class level and can be used anywhere in the JSP page.

16

• Example: <%@page contentType="text/html" %> <%! int cnt=0; private int getCount(){ //increment cnt and return the value cnt++; return cnt; } %>

Values of Cnt are:

<%=getCount()%>

<%=getCount()%>

<%=getCount()%>

<%=getCount()%>

<%=getCount()%>

<%=getCount()%>

17

INTRODUCTION TO JSP SCRIPTLETS •



Syntax of JSP Scriptles are: <% //java codes %> JSP Scriptlets begins with <% and ends %> .We can embed any amount of java code in the JSP Scriptlets. JSP Engine places these code in the _jspService() method. Variables available to the JSP Scriptlets are: request: request represents the clients request and is a subclass of HttpServletRequest. Use this variable to retrieve the data submitted along the request. Example: <% //java codes String userName=null; userName=request.getParameter("userName"); %>



response: response is subclass of HttpServletResponse.



session: session represents the HTTP session object associated with the request.



out: out is an object of output stream and is used to send any output to the client.



Other variable available to the scriptlets are pageContext, application,config and exception. 18

INTRODUCTION TO JSP EXPRESSIONS

• Syntax of JSP Expressions are: <%="Any thing" %> • JSP Expressions start with Syntax of JSP Scriptles are with <%= and ends with %>. Between these this you can put anything and that will converted to the String and that will be displayed. • Example: <%="Hello World!" %> Above code will display 'Hello World!'. 19

JSP Date Example •

Source Code:

<%@page contentType="text/html" import="java.util.*" %>

 

  Date Example
  Current Date and time is:  <%= new java.util.Date() %>
20

Reading Request Information • When an HTTP client such as web browser sends a request to a wen server, along with the request it also sends some HTTP variables like Remote address, Remote host, Content type etc. In some cases these variables are useful to the programmers. • Example: C:\Documents and Settings\Srujan\Desktop\jdbc\jsp ppt\request.txt

21

Retrieving the data posted to a JSP file from HTML file • Consider an html page that prompts the user to enter his/her name, let's call it getname.html. Enter your name

 

Enter your name:

22

• The target of form is "showname.jsp", which displays the name entered by the user. <%@page contentType="text/html" %>

Welcome :  <%=request.getParameter("username")%>

23

JSP Sessions • In any web application user moves from one page to another and it becomes necessary to track the user data and objects throughout the application. JSP provide an implicit object "session", which can be use to save the data specific the particular to the user. Q: we will create an application that takes the user name from the user and then saves into the user session. We will display the saved data to the user in another page. 24

• savenameform.jsp This takes the input from user <%@ page language="java" %> Name Input Form

Enter Your Name:

25

• code of savenametosession.jsp: <%@ page language="java" %> <% String username=request.getParameter("username"); if(username==null) username=""; session.setAttribute("username",username); %> Name Saved

Next Page to view the session value

26

• code of showsessionvalue.jsp: <%@ page language="java" %> <% String username=(String) session.getAttribute("username"); if(username==null) username=""; %> Show Saved Name

Welcome: <%=username%>



27

JSP Cookies • Cookies are short pieces of data sent by web servers to the client browser. The cookies are saved to clients hard disk in the form of small text file. Cookies helps the web servers to identify web users, by this way server tracks the user. Cookies play very important role in the session tracking.

28

Cookie Class • In JSP cookie are the object of the class javax.servlet.http.Cookie. This class is used to creates a cookie, a small amount of information sent by a servlet to a Web browser, saved by the browser, and later sent back to the server. A cookie's value can uniquely identify a client, so cookies are commonly used for session management. A cookie has a name, a single value, and optional attributes such as a comment, path and domain qualifiers, a maximum age, and a version number. 29

Cookie Objects • getComment() Returns the comment describing the purpose of this cookie, or null if no such comment has been defined. • getMaxAge() Returns the maximum specified age of the cookie. • getName() Returns the name of the cookie.getPath()Returns the prefix of all URLs for which this cookie is targeted. • getValue() Returns the value of the cookie. 30

• setComment(String) If a web browser presents this cookie to a user, the cookie's purpose will be described using this comment. • setMaxAge(int) Sets the maximum age of the cookie. The cookie will expire after that many seconds have passed. Negative values indicate the default behavior: the cookie is not stored persistently, and will be deleted when the user web browser exits. A zero value causes the cookie to be deleted • setPath(String) This cookie should be presented only with requests beginning with this URL. • setValue(String) Sets the value of the cookie. Values with various special characters (white space, brackets and parentheses, the equals sign, comma, double quote, slashes, question marks, the "at" sign, colon, and semicolon) should be avoided. Empty values may not behave the same way on all browsers. 31

Example Using Cookies • cookieform.jsp which prompts the user to enter his/her name. <%@ page language="java" %> Cookie Input Form

Enter Your Name:

32



setcookie.jsp file, which sets the cookie <%@ page language="java" import="java.util.*"%> <% String username=request.getParameter("username"); if(username==null) username=""; Date now = new Date(); String timestamp = now.toString(); Cookie cookie = new Cookie ("username",username); cookie.setMaxAge(365 * 24 * 60 * 60); response.addCookie(cookie); %> Cookie Saved

Next Page to view the cookie value

33



Code for showcookievalue.jsp <%@ page language="java" %> <% String cookieName = "username"; Cookie cookies [] = request.getCookies (); Cookie myCookie = null; if (cookies != null) { for (int i = 0; i < cookies.length; i++) { if (cookies [i].getName().equals (cookieName)) { myCookie = cookies[i]; break; } } } %> <% if (myCookie == null) { %> No Cookie found with the name <%=cookieName%> <% } else { %>

Welcome: <%=myCookie.getValue()%>. <% } %> 34

Disabling Session in JSP • <%@ page language="java" session="false"%> Session Disabled

Session is Disabled in this page

35

USING BEANS IN JSP •

Java Beans are reusable components. They are used to separate Business logic from the Presentation logic. Internally, a bean is just an instance of a class.

JSP’s provide three basic tags for working with Beans •

<jsp:useBean id=“bean name” class=“bean class” scope = “page | request | session |application ”/> bean name = the name that refers to the bean. Bean class = name of the java class that defines the bean. 36

2. <jsp:setProperty name = “id” property = “someProperty” value = “someValue” /> id = the name of the bean as specified in the useBean tag. property = name of the property to be passed to the bean. value = value of that particular property . An variant for this tag is the property attribute can be replaced by an “ * ”. What this does is that it accepts all the form parameters and thus reduces the need for writing multiple setProperty tags 37

3. <jsp:getProperty name = “id” property = “someProperty” /> Here the property is the name of the property whose value is to be obtained from the bean.

38


Related Documents

Srujan Jsp
December 2019 9
Srujan
October 2019 7
Srujan 1
October 2019 11
Jsp
April 2020 36
Jsp
May 2020 27
Jsp
May 2020 14

More Documents from ""

Cascading Style Sheets (css)
December 2019 56
Javascript.st1
December 2019 76
Jsp
December 2019 66
Jf4_60
December 2019 53