Hinweise zu Servlets

Werbung
Servlets und Jakarta-Tomcat
IFIS, 8. November 2006
Literatur: onjava.com (O'RELLY)
Java ist auch eine Insel (online)
.....
Web Application Directory Structure
/beispiel
/beispiel/WEB-INF
/beispiel/WEB-INF/classes
/beispiel/WEB-INF/lib
Deployment Desciptor (web.xml)
Zu positionieren in: /beispiel/WEB-INF
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">
<web-app>
</web-app>
Creating a Web Application
ServletContext
Änderung des 'server.xml'-Files in /conf/
<Context path="/beispiel" docBase="beispiel" debug="0"
reloadable="true" />
Servlet in /beispiel/WEB-INF/classes
package com.beispiel;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class login extends HttpServlet {
private String target = "/welcome.jsp";
private String getUser(String username, String password) {
// Just return a static name
// If this was reality, we would perform a SQL lookup
return "Bob";
}
public void init(ServletConfig config)
throws ServletException {
super.init(config);
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
// Get the password from the request
String password = request.getParameter("password");
String user = getUser(username, password);
// Add the fake user to the request
request.setAttribute("USER", user);
// Forward the request to the target named
ServletContext context = getServletContext();
RequestDispatcher dispatcher =
context.getRequestDispatcher(target);
dispatcher.forward(request, response);
}
}
Herunterladen