Java Tutorial/Servlet/web.xml

Материал из Java эксперт
Версия от 17:44, 31 мая 2010; (обсуждение)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Define welcome files for web application

<?xml version="1.0" encoding="UTF-8"?>
<web-app 
        xmlns="http://java.sun.ru/xml/ns/javaee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.ru/xml/ns/javaee http://java.sun.ru/xml/ns/javaee/web-app_2_5.xsd" 
        version="2.5">
    
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
</web-app>





Servlet Map URL in web.XML

<HTML>
<HEAD>
<TITLE>Introductions</TITLE>
</HEAD>
<BODY>
<FORM METHOD=GET ACTION="Hello.do">
If you don"t mind me asking, what is your name?
<INPUT TYPE=TEXT NAME="name"><P>
<INPUT TYPE=SUBMIT>
</FORM>
</BODY>
</HTML>





Servlet Pass Init Value in Web XML

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class MyServlet extends HttpServlet {
  int count;
  public void init() throws ServletException {
    String initial = getInitParameter("initial");
    try {
      count = Integer.parseInt(initial);
    }
    catch (NumberFormatException e) {
      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 (and with a possible initialization");
    out.println("parameter figured in), this servlet has been accessed");
    out.println(count + " times.");
  }
}





Servlet Web XML Context Init Parameter

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet extends HttpServlet {
   
  private String dbName="";
  private String dbPassword="";
  public void init(ServletConfig config) throws ServletException  {
    super.init(config);
    ServletContext context = getServletContext();
    dbName = context.getInitParameter("name");
    dbPassword = context.getInitParameter("password");

  }
  public void doGet (HttpServletRequest req, HttpServletResponse res) throws IOException
  {
    ServletOutputStream out = res.getOutputStream();
    res.setContentType("text/html");
    out.println("<html><head><title>Basic Servlet</title></head>");
    out.println("<body>Database username is  <b>" + dbName);
    out.println("</b><br>Database password is  <b>" + dbPassword + "</b>");
    out.println("</body></html>");
  }
}





Set error page in web.xml

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.ru/dtd/web-app_2_3.dtd">
<web-app>

    <error-page>
        <error-code>
            404
        </error-code>
        <location>
            /404.html
        </location>
    </error-page>
</web-app>





Set Servlet ContextListener in web.XML

import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet implements ServletContextListener {
  public void contextInitialized(ServletContextEvent e) {
//    Connection con =     // create connection
  //    e.getServletContext().setAttribute("con", con);
      System.out.println("contextInitialized(ServletContextEvent e)");
  }
  public void contextDestroyed(ServletContextEvent e) {
//    Connection con =
  //     (Connection) e.getServletContext().getAttribute("con");
    //try { con.close(); } 
    //catch (SQLException ignored) { } // close connection
    System.out.println("contextDestroyed(ServletContextEvent e)");
  }
}