servlets

14
Servlets

Upload: dara

Post on 05-Jan-2016

43 views

Category:

Documents


6 download

DESCRIPTION

Servlets. Servlets. A form. The HTML source. Chapter 1 Please enter your name and password then press start - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Servlets

Servlets

Page 2: Servlets

Servlets

Page 3: Servlets

A form

Page 4: Servlets

The HTML source<html>

<head>

<title>Chapter 1</title>

</head>

<body><font size="4">

<center>Please enter your name and password then press start<br>

<form method="GET" action="http://localhost:8080/servlet/GetDemo" >

Name: <input name="uname" value="" type="text" size="20"> <br>

Password: <input name="userpw" value="" type="password" size=10>

<input value="Start" type="submit" > <br>

</form>

</center>

<hr>

</body>

</html>

Page 5: Servlets

A servletimport java.io.*;import java.net.*;

import javax.servlet.*;import javax.servlet.http.*;

public class GetDemo extends HttpServlet { protected void processRequest(HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException {

response.setContentType("text/html"); PrintWriter out = response.getWriter(); String username = request.getParameter("uname"); String password = request.getParameter("userpw");

Page 6: Servlets

out.println("<HTML>"); out.println("<HEAD><TITLE>GetDemo Output</TITLE></HEAD>"); out.println("<BODY>"); out.println("Hello " + username + "<br>"); out.println("Your password was: " + password + "<br>"); out.println("</BODY>"); out.println("</HTML>"); out.close(); } protected void doGet(HttpServletRequest request, HttpServletResponse

response) throws ServletException, IOException { processRequest(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse

response) throws ServletException, IOException { processRequest(request, response); } public String getServletInfo() { return "Short description"; }}

Page 7: Servlets

Servlet ContainerYou need a servlet container for this. E.g. Apache Tomcat.

Download the preconfigured Apache Tomcat from the course website.Unzip

Will create two directories, "apache-tomcat-6.0.18" and "development"

Starting/Stopping Tomcat

If you are working on Windows, open fileapache-tomcat-6.0.18\bin\startup.batand modify line:set JAVA_HOME=C:\Program Files\Java\jdk1.5.0_09to reflect your local path of java jdk. (Do the same for the shutdown.bat)

If you are working on the lab machines (Linux) execute first

chmod +x apache-tomcat-6.0.18/bin/*.sh

Page 8: Servlets

Servlet Container Then start Apache Tomcat by double clicking on

apache-tomcat-6.0.18\bin\startup.bat(In Windows)or executeapache-tomcat-6.0.18/bin/startup.sh(in Linux)

To stop Tomcat execute the shutdown.bat in Windows, shutdown.sh in Linux.

Page 9: Servlets

Servlet ContainerYour HTML files should go under:apache-tomcat-6.0.18/webapps/ROOT(In Linux)apache-tomcat-6.0.18\webapps\ROOT(In Windows)

Then to open an HTML file in the browser, specify e.g.http://localhost:8080/GetExample.html

Your Java classes should go under:apache-tomcat-6.0.18/webapps/ROOT/WEB-INF/classes(In Linux)apache-tomcat-6.0.18\webapps\ROOT\WEB-INF\classes(In Windows)

Any jar file (such as classes12.jar of Oracle JDBC) should go under:apache-tomcat-6.0.18/webapps/ROOT/WEB-INF/lib(In Linux)apache-tomcat-6.0.18\webapps\ROOT\WEB-INF(In Windows)

Page 10: Servlets

Servlet ContainerCopy/sftp the /opt/oracle/jar/classes12.jar to your development directory.

To compile, cd to the 'development' directory and execute the following:javac -d

../apache-tomcat-6.0.18/webapps/ROOT/WEB-INF/classes -classpath ../apache-tomcat-6.0.18/lib/servlet-api.jar:classes12.jar *.java

(In Linux)javac -d ..\apache-tomcat-6.0.18\webapps\ROOT\WEB-INF\

classes -classpath ..\apache-tomcat-6.0.18\lib\servlet-api.jar;classes12.jar *.java

(In Windows)These commands will copy your .class files to the ..\apache-tomcat-

6.0.18\webapps\ROOT\WEB-INF\classes directory.

Page 11: Servlets

Connectionmanager

import java.sql.*;import java.util.*;

public class ConnectionManager { private static ConnectionManager instance = null; private Stack connections; private ConnectionManager () { connections = new Stack(); try {

DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver()); } catch (Exception ex) { System.out.println(ex); } }

public static ConnectionManager getInstance() { if (instance == null) instance = new ConnectionManager(); return instance; }

Page 12: Servlets

Connectionmanager

public Connection getConnection() { Connection conn = null; if (!connections.empty()) conn = (Connection) connections.pop(); else { //No one left in the stack, create a new one try { conn = DriverManager.getConnection

("jdbc:oracle:thin:@localhost:1521:TEACH", "user", "passw");

} catch (SQLException ex) { System.out.println("SQLException: " + ex); } } return conn; } public void returnConnection(Connection conn) { if (conn != null) connections.push(conn); } }

This is assuming the ssh tunel is used. Otherwise, if running on the lab machines, replace by

("jdbc:oracle:thin:@oracle.csc.uvic.ca:1521:TEACH", "user", "passw")

Page 13: Servlets

SSH Tunnel

In order to be able to connect to ORACLE remotely from your machine at home execute (in your machine):

ssh2 -L 1521:oracle.csc.uvic.ca:1521 <yourusername>@u-knoppix.csc.uvic.ca

Then the oracle machine is accessed as “localhost”

Use some other lab machine.

Might be called just ssh in your machine.

Page 14: Servlets

Example of using ConnectionManager

String statementString = "SELECT * FROM movies"; //No ; in the end of query

Connection conn = ConnectionManager.getInstance().getConnection(); try { Statement stmt = conn.createStatement(); stmt. executeQuery(statementString); stmt.close(); } catch(SQLException e) { out.println(e); } ConnectionManager.getInstance().returnConnection(conn);