Pace CS 396S - Java Server Pages and Java Beans

Unformatted text preview:

Java Server Pages and Java BeansJava Server PagesJava BeansSimple Hello ExampleExample for Finding an AddressGrocery Store DatabaseJava Server Pages and Java BeansJava server pages (JSP) and Java beans1 work together to create a web application. Java server pages are html pages that also contain regular Java code. This code is included between special tags that begin with‘<%’. Java beans are Java programs that follow some specific rules. Together they make up part of a web application.There are advantages and disadvantages to using Java server pages and beans. The main advantage is thatthe JSP contains the HTML code and not the bean. This keeps the Java bean ‘clean’. The disadvantage isthat there are a number of conventions for Java server pages that must be followed.Java server pages are a combination of HTML and Java code. They have to be translated into a Java servlet, which is then compiled, before they can be accessed. This is done the first time that a page is requested. After that, the compiled code resides on the server and can be used as is by any succeeding requests. On a stand-alone system, you can find both the servlet and the class file in the folder work/Catalina/localhost/_. Later we will see how to include these within the application folder itself.Java Server PagesIn a JSP file the Java code is contained between tags that begin with <% and end with %>. These tags arealso used in active server pages (asp). There are several different kinds of JSP tags depending upon their use.- <%= … %> is used for expressions.- <%! … %> is used for declarations.- <% … %> is used for straight Java code.- <%@ … %> is used to include another file such as an HTML file or a package such as java.sql.*.There are some reserved words that are used by JSP files without further definition. These should be familiar from similar terms used with Java servlets.- request – an instance of HttpServletRequest.- response – an instance of HttpServletResponse.- out – a PrintWriter object for the response.- session – the HttpSession object associated with the session.- application – an instance of ServletContextJava BeansJava beans are regular Java programs with several specific restrictions. The constructor may not have anyparameters, and the variables all have get and set accessor and mutator methods. The Java server page uses the accessor and mutator methods of the bean to send values to the bean and to get resulting data back from the bean.1 For more information about JSP and Java beans see Marty Hall & Larry Brown, Core Servlets and Java Server Pages, First Edition, Sun Microsystems Press/Prentice-Hall PTR Book, 2003.1In a Java bean, you can instantiate other classes, access databases, create lists, tables, and anything else you might want to do. You can also have methods that receive request data from the JSP file. They have the usual request parameter as in the following example:public void processRequest (HttpServletRequest request) { … }Java server pages are usually in the root folder, while class files go in the same classes folder as the servlet classes.Simple Hello ExampleThe first example uses a JSP file called hello.jsp, an HTML file called hello.html, and a Java bean called HelloBean.java. The HTML file has a form that sends request data to the JSP file. The JSP file in turn sends the data on to the bean. The bean uses its mutator methods (sets) to receive the data. The bean thenstores the data in its instance variables and returns the data to the JSP file using its accessor methods (gets).The following shows the HTML file and the way it is displayed in a browser.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><title>Hello</title></head><body><h3>Enter your name and email address: </h3><form method="get" action="hello.jsp"><p><input type="text" name="name" value="" size="20"/> Name </p><p><input type="text" name="email" value="" size="20"/> Email </p><p><input type="submit" name="Send" value="Send"/> </p></form></body></html>2The JSP file comes next. It could be made simpler, but as it is, it demonstrates some JSP tags. The first one is used for a declaration, here for two strings, name and email. This is followed by the line that tells the server where to find the bean. This is done with tags that follow XML syntax. They are case sensitiveand must have closing tags or a closing ‘/’. <jsp:useBean id="hello" scope="session" class="greetings.HelloBean" />This says that the bean is called HelloBean and it is in the package, greetings. The id is used throughout the JSP file to name this particular bean.The request data from the HTML file uses standard servlet code (JSP files are translated into servlets).<jsp:setProperty name="hello" property="name" value='<%= request.getParameter ("name") %>'/><jsp:setProperty name="hello" property="email" value='<%= request.getParameter ("email") %>'/>The name, hello, refers to the bean. These say to set the bean properties, name and email. The property names must be the same as those in the bean, and the parameter names must agree exactly with those in the HTML file.The rest of the JSP file just echoes the data back to the browser. It supplies the HTML code for the outputpage.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><title>Hello JSP</title></head><body><%! String name, email; %><jsp:useBean id="hello" scope="session" class="greetings.HelloBean" /><jsp:setProperty name="hello" property="name" value='<%= request.getParameter ("name") %>'/><jsp:setProperty name="hello" property="email" value='<%= request.getParameter ("email") %>'/>3<% name = hello.getName();email = hello.getEmail();out.println ("<h3>Hello, your name is " + name);out.println (" and your email address is " + email + ".</h3>");%></body></html>The result looks like the following in the browser.Finally the bean for this example is very simple. It just stores the data using its mutator methods and returns it using the accessor methods. It does not have a constructor or any methods other than the gets and sets. A more realistic example would do something with the data before returning it.public class HelloBean{private String name = "";private String email = "";public String getName() {return name;}public String getEmail() {return email;}public void setName (String n) {name = n;}public void setEmail (String e) {email = e;}} // HelloBeanNaming for the variables and get


View Full Document
Download Java Server Pages and Java Beans
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Java Server Pages and Java Beans and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Java Server Pages and Java Beans 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?