Download - Cis 274 intro
![Page 1: Cis 274 intro](https://reader033.vdocuments.mx/reader033/viewer/2022052411/5562f914d8b42a213b8b569a/html5/thumbnails/1.jpg)
CIS – 274:Internet Programming
Lab Session #1
Intro and Basics
T.A. : Aren ZomorodianE-Mail: [email protected] : aren.z
![Page 2: Cis 274 intro](https://reader033.vdocuments.mx/reader033/viewer/2022052411/5562f914d8b42a213b8b569a/html5/thumbnails/2.jpg)
OutlineI
• Servlets
II• Java Server Pages (JSP)
III• Preparing the Dev. Enviornment
IV• Web-App Folders and Hierarchy
V• Writing the First Servlet
VII
• Compiling
VIII
• Deploying a Sample Servlet (Packaged & Unpackaged)
VI• Writing the First JSP
![Page 3: Cis 274 intro](https://reader033.vdocuments.mx/reader033/viewer/2022052411/5562f914d8b42a213b8b569a/html5/thumbnails/3.jpg)
A servlet is a Java programming language class used to extend the capabilities of servers that host applications accessed via a request-response programming model
A servlet is like any other java class
Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by Web servers
Servlets
![Page 4: Cis 274 intro](https://reader033.vdocuments.mx/reader033/viewer/2022052411/5562f914d8b42a213b8b569a/html5/thumbnails/4.jpg)
Use regular HTML for most of page
Mark dynamic content with special tags
A JSP technically gets converted to a servlet but it looks more like PHP files where you embed the java into HTML.
The character sequences <% and %> enclose Java expressions, which are evaluated at run time
<%= new java.util.Date() %>
Java Server Pages (JSP)
![Page 5: Cis 274 intro](https://reader033.vdocuments.mx/reader033/viewer/2022052411/5562f914d8b42a213b8b569a/html5/thumbnails/5.jpg)
Preparing
The
Development
Environment
![Page 6: Cis 274 intro](https://reader033.vdocuments.mx/reader033/viewer/2022052411/5562f914d8b42a213b8b569a/html5/thumbnails/6.jpg)
Java Development Kit (JDK)◦ http://www.oracle.com/technetwork/java/javase/downloads/index.html
Apache Tomcat webserver◦ http://tomcat.apache.org/download-70.cgi
Set JAVA_HOME Environmental Variable◦ Right click on “Computer” (Win 7), and click on “Properties”◦ Click on “Advanced System Settings” on top right corner◦ On the new window opened click on “Environment Variables” ◦ In the “System Variables” section, the upper part of the window, click on “New…”
Variable name: JAVA_HOME Variable value: Path to the jdk directory (in my case C:\Program Files\Java\jdk1.6.0_21)
◦ Click on “OK”
Checklist
![Page 7: Cis 274 intro](https://reader033.vdocuments.mx/reader033/viewer/2022052411/5562f914d8b42a213b8b569a/html5/thumbnails/7.jpg)
Set CATALINA_HOME Environmental Variable◦ Right click on “Computer” (Win 7), and click on “Properties”◦ Click on “Advanced System Settings” on top right corner◦ On the new window opened click on “Environment Variables” ◦ In the “System Variables” section, the upper part of the window, click on “New…”
Variable name: CATALINA_HOME Variable value: Path to the apache-tomcat directory (in my case D:\Servers\apache-tomcat-
7.0.12-windows-x86\apache-tomcat-7.0.12)
◦ Click on “OK”
Note: You might need to restart your computer after adding environmental variables to make changes to take effect
Checklist (cont.)
![Page 8: Cis 274 intro](https://reader033.vdocuments.mx/reader033/viewer/2022052411/5562f914d8b42a213b8b569a/html5/thumbnails/8.jpg)
In your browser type: localhost:8080◦ If tomcat’s page is opened then the webserver installation was successful
Check JAVA_HOME variable: ◦ in command prompt type: echo %JAVA_HOME%
◦ Check to see the variable value and if it is set correctly
Checkup
![Page 9: Cis 274 intro](https://reader033.vdocuments.mx/reader033/viewer/2022052411/5562f914d8b42a213b8b569a/html5/thumbnails/9.jpg)
Folder Placement And Hierarchy All the content should be placed under tomcat’s “webapps”
directory
![Page 10: Cis 274 intro](https://reader033.vdocuments.mx/reader033/viewer/2022052411/5562f914d8b42a213b8b569a/html5/thumbnails/10.jpg)
Folder Placement And Hierarchy (cont.)
• $CATALINA_HOME\webapps\helloservlet": This directory is known as context root for the web context "helloservlet". It contains the resources that are visible by the clients, such as HTML, CSS, Scripts and images. These resources will be delivered to the clients as it is. You could create sub-directories such as images, css and scripts, to further categories the resources accessible by clients.
• "$CATALINA_HOME\webapps\helloservlet\WEB-INF": This is a hidden directory that is used by the server. It is NOT accessible by the clients directly (for security reason). This is where you keep your application-specific configuration files such as "web.xml" (which we will elaborate later). It's sub-directories contain program classes, source files, and libraries.
• "$CATALINA_HOME\webapps\helloservlet\WEB-INF\src": Keep the Java program source files. It is optional but a good practice to separate the source files and classes to facilitate deployment.
• "$CATALINA_HOME\webapps\helloservlet\WEB-INF\classes": Keep the Java classes (compiled from the source codes). Classes defined in packages must be kept according to the package directory structure.
• "$CATALINA_HOME\webapps\helloservlet\WEB-INF\lib": keep the libraries (JAR-files), which are provided by other packages, available to this webapp only.
![Page 11: Cis 274 intro](https://reader033.vdocuments.mx/reader033/viewer/2022052411/5562f914d8b42a213b8b569a/html5/thumbnails/11.jpg)
Sample Servlet (form.html)
<HTML>
<HEAD>
<TITLE>Introductions</TITLE>
</HEAD>
<BODY>
<FORM METHOD=GET ACTION="/servlet/Hello">
If you don't mind me asking, what is your name?
<INPUT TYPE=TEXT NAME="name"><P>
<INPUT TYPE=SUBMIT>
</FORM>
</BODY>
</HTML>
![Page 12: Cis 274 intro](https://reader033.vdocuments.mx/reader033/viewer/2022052411/5562f914d8b42a213b8b569a/html5/thumbnails/12.jpg)
Sample Servlet (Hello.java)
import java.io.*;import javax.servlet.*;import javax.servlet.http.*;
public class Hello extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
res.setContentType("text/html"); PrintWriter out = res.getWriter();
String name = req.getParameter("name"); out.println("<HTML>"); out.println("<HEAD><TITLE>Hello, " + name + "</TITLE></HEAD>"); out.println("<BODY>"); out.println("Hello, " + name); out.println("</BODY></HTML>"); }
public String getServletInfo() { return "A servlet that knows the name of the person to whom it's" + "saying hello"; }}
![Page 13: Cis 274 intro](https://reader033.vdocuments.mx/reader033/viewer/2022052411/5562f914d8b42a213b8b569a/html5/thumbnails/13.jpg)
Sample Servlet (web.xml)
The web.xml file defines each servlet and JSP page within a Web Application.
The file goes into the WEB-INF directory
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app> <servlet> <servlet-name> hi </servlet-name> <servlet-class> HelloWorld </servlet-class> </servlet> <servlet-mapping> <servlet-name> hi </servlet-name> <url-pattern> /hello.html </url-pattern> </servlet-mapping></web-app>
![Page 14: Cis 274 intro](https://reader033.vdocuments.mx/reader033/viewer/2022052411/5562f914d8b42a213b8b569a/html5/thumbnails/14.jpg)
Compiling a Servlet Compile the packages:
"C:\Program Files\Java\jdk1.6.0_17\bin\javac" <Package Name>\*.java -d .\
If external (outside the current working directory) classes and libraries are used, we will need to explicitly define the CLASSPATH to list all the directories which contain used classes and libraries
set CLASSPATH=C:\lib\jars\classifier.jar ;C:\UserProfiling\jars\profiles.jar
-d (directory)◦ Set the destination directory for class files. The destination directory must
already exist.◦ If a class is part of a package, javac puts the class file in a subdirectory
reflecting the package name, creating directories as needed.
-classpath◦ Set the user class path, overriding the user class path in the CLASSPATH environment
variable. ◦ If neither CLASSPATH or -classpath is specified, the user class path consists of the
current directory
java -classpath C:\java\MyClasses utility.myapp.Cool
Click Here For Description of More Options
![Page 15: Cis 274 intro](https://reader033.vdocuments.mx/reader033/viewer/2022052411/5562f914d8b42a213b8b569a/html5/thumbnails/15.jpg)
Javac –classpath .;..\classes;”D:\Servers\apache-tomcat-6.0.26-windows-x86\apache-tomcat-6.0.26\lib\servlet-api.jar” src\*.java –d ./test
Compiling a Servlet (cont.)
What does the following command do?
![Page 16: Cis 274 intro](https://reader033.vdocuments.mx/reader033/viewer/2022052411/5562f914d8b42a213b8b569a/html5/thumbnails/16.jpg)
Now
Try it Yourself
(Both Packaged and
Unpackaged)
![Page 17: Cis 274 intro](https://reader033.vdocuments.mx/reader033/viewer/2022052411/5562f914d8b42a213b8b569a/html5/thumbnails/17.jpg)
import java.io.*;import javax.servlet.*;import javax.servlet.http.*;
public class SimpleCounter extends HttpServlet {
int count = 0;
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/plain"); PrintWriter out = res.getWriter(); count++; out.println("Since loading, this servlet has been accessed " + count + " times."); }}
Simple Counter
![Page 18: Cis 274 intro](https://reader033.vdocuments.mx/reader033/viewer/2022052411/5562f914d8b42a213b8b569a/html5/thumbnails/18.jpg)
<HTML>
<BODY>
Hello! The time is now <%= new java.util.Date() %>
</BODY>
</HTML>
Writing the First JSP
<HTML><BODY>Hello, world</BODY></HTML>
Same as HTML, but just save it with .jsp extension
HTMLJSP
![Page 19: Cis 274 intro](https://reader033.vdocuments.mx/reader033/viewer/2022052411/5562f914d8b42a213b8b569a/html5/thumbnails/19.jpg)
AUA – CoEApr.11, Spring 2012
CIS-274: Internet ProgrammingIntro
END---
CIS 274 – Internet Programming
Lab Session #1
![Page 20: Cis 274 intro](https://reader033.vdocuments.mx/reader033/viewer/2022052411/5562f914d8b42a213b8b569a/html5/thumbnails/20.jpg)
Previously Asked Questions
Hashmamp vs. Hashtable
1. Hashtable is synchronized, whereas HashMap is not. 1. This makes HashMap better for non-threaded applications, as
unsynchronized Objects typically perform better than synchronized ones.
2. Hashtable does not allow null keys or values. HashMap allows one null key and any number of null values.