Beruflich Dokumente
Kultur Dokumente
Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project
JSP is a java based technology used to simplify the development of dynamic web pages. JSP is used to separate dynamic content of web page from its content with the help of HTML and JSP tags. Current version is 2.1.
5. Destroying stage
Java classes (.class) Java Libraries (.jar) Configuration files Anything web-related - Directories - JavaServer Pages (JSP) - HTML - Css files - JavaScript Files - Etc.
Exercise 1
Develope the first java web application 1. Download the file: jspservlet-00.zip
2.
3. 4.
Unzip it
Import from Eclipse Run it
Exercise 1 Results
You should get this result
Exercise 1 Analisis
The web.xml file
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" . . . version="2.5">
<servlet> <display-name>HelloWorldServlet</display-name> <servlet-name>HelloWorldServlet</servlet-name> <servlet-class> com.example.servlets.HelloWorldServlet </servlet-class> </servlet> <servlet> <display-name>AnotherServlet</display-name> <servlet-name>AnotherServlet</servlet-name> <servlet-class> com.example.servlets.AnotherServlet </servlet-class> </servlet>
Servlet declarations
Exercise 1 Analisis
The web.xml file . . . <servlet-mapping> <servlet-name>HelloWorldServlet</servlet-name> <url-pattern>/HelloWorldServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>AnotherServlet</servlet-name> <url-pattern>/AnotherServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
Servlet mappings
Exercise 1 Analisis
Servlet classes (other might be here classes too)
JSP page consist of elements and templates data .These elements are described in different elements type are: Directives:- JSP directives provides the page resources and their properties. Some are as: 1. Page directive:-Used to provide page specific properties E g : < % @ p a ge c o n t e n t T y p e = t e x t / h t m l l a n g u a g e = j a v a % > Attributes are:-
1.
2. 3. 4. 5.
Autoflush=true/false
Buffer=in KB contentType=info errorPage=page url e x t e n d s = cl a s s n a m e
6.
7. 8. 9.
i m p o r t = p a ck a g e s e p a r a t e d b y c o m m a
info=infotext isELIgnored=true/false i s E r r o r P a g e = p a ge n a m e u r l
1 0 . i s T h r e a d S a f e = t r u e / f a l s e
11. language=java
1 2 . p a g e E n c o d i n g = I S O - 8 8 5 9 - 1 1 3. session=true/false
2. Include Directives:The include directives tells the container to add the defined resource content inline to the JSP page during the translation time .This tag used to make reusability. Eg :<%@ include file=header.html %>
3. Taglib Directives:Taglib tag is used to include external tag library in your web page.
Attributes are:1.Uri= url of taglibrary 2.tagDir=tag library directory 3.prefix=like object for library
1. <%! This is a declaration %> 2. <% this is a scriptlet %> 3. <% = this is an expression %>
Action elements are certain directives that are given to the container perform certain task during the execution of a JSP page.
this action used to access the defines property to bean object.But it is important to declare bean object using useBean tag.
Attributes are:1. name :- The name of object where to retrieve object properties. 2. property: - Name of property to get. Eg:-
<jsp:getProperty name=beannameproperty=propertyname>
this action used to access the defines property to bean object.But it is important to declare bean object using useBean tag.
Attributes are:1. name :- The name of object where to retrieve object properties. 2. property: - Name of property to get. Eg:-
<jsp:getProperty name=beannameproperty=propertyname>
name of property value to be set we can also use * to specify all the property of bean.but in this case you have to specify the name of components as bean property.
3.Param:- The name of the request parameter whose value you want to give to a bean property.this name basically come from web form. 4.Value:- the value to be assign to this property. Eg:<jsp:setProperty name=beanname property=propertyname value=tobeset > =propertyname>
We are creating a login screen with java bean which check the username and password and return the result according to input
Index.jsp <%-Document : index Created on : Apr 2, 2012, 12:23:12 PM Author : BARDHAN --%>
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <form action="check.jsp" method="POST"> Enter name <input type="text" name="name" /><br> Enter Password <input type="password" name="password" /> <br/> <input type="submit" value="LOGIN" /> <input type="reset" value="reset" /> </form> </body> </html>
Check.jsp Page:<%-Document : check Created on : Apr 2, 2012, 12:29:55 PM Author : BARDHAN --%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <jsp:useBean id="login" class="beans.login" scope="page"> <jsp:setProperty name="login" property="*" /> </jsp:useBean> Your Name:<jsp:getProperty name="login" property="name" /> <br> Your Password is:<jsp:getProperty name="login" property="password" /> <%boolean ba=login.getResult(); if(ba) { response.sendRedirect("welcome.jsp"); } else{ out.println("This is not right username or password"); } %> </body> </html>
Bean.login bean:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package beans;
/** * * @author BARDHAN */ public class login { private String name; private String password; private boolean result; public String getName() { return name; }
} }
This action element is used to pass the parameter to other action elements. For example
<jsp:forward page=hello.jsp>