tomcat学习笔记

◎使用tomcat bin下的start方法在tomcat出现错误时,不会留有任何调试信息,而且dos窗口关闭
使用tomcat bin下的run方法则在tomcat出现错误时,会留有任何调试信息,而且dos窗口仍打开
◎可以通过httpservletrequest的方法getServletContext获得servletcontext,然后通过servletcontext的getRequestDispatcher来获得RequestDispatch对象,通过该对象的forward使得当前的servlet将控制权转向其他jsp或者servlet
//forward的使用
具体代码:
public void doPost(HttpServletRequest request,
    HttpServletResponse response)
    throws ServletException, IOException {

    // Get the username from the request
    String username = request.getParameter("username");
    // Get the password from the request
    String password = request.getParameter("password");

    // Add the  user to the request
    request.setAttribute("USER", username);
    request.setAttribute("PASSWORD", password);

    // Forward the request to the target named
    ServletContext context = getServletContext();

    System.out.println("Redirecting to " + target);
    RequestDispatcher dispatcher =
      context.getRequestDispatcher(target);
    dispatcher.forward(request, response);
  }
流程:httpservletrequest->servletContext->RequestDispatcher
◎一个taglib的开发实例
1。定义taglib
2。定义jsp tag
3。web.xml文件中定义taglib语句
4。定义tag文件
1.
package mypack;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;

public class HelloTag extends TagSupport
{
    public  HelloTag() {

    }

    // Method called when the closing hello tag is encountered
    public int doEndTag() throws JspException {

        try {

            // We use the pageContext to get a Writer
            // We then print the text string Hello
            pageContext.getOut().print("Hello");
        }
        catch (Exception e) {

            throw new JspTagException(e.getMessage());
        }
        // We want to return SKIP_BODY because this Tag does not support
        // a Tag Body
        return SKIP_BODY;
    }

    public void release() {

        // Call the parent's release to release any resources
        // used by the parent tag.
        // This is just good practice for when you start creating
        // hierarchies of tags.
        super.release();
     
    }
}

2.
<%@ taglib uri="/mytaglib" prefix="mm" %>


  helloapp


  : <%= request.getAttribute("USER") %>


3.

  '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN'
  'http://java.sun.com/j2ee/dtds/web-app_2_3.dtd'>

 
        dispatcher
        mypack.DispatcherServlet
  

  
        dispatcher
        /dispatcher
  


 
    /mytaglib
    /WEB-INF/mytaglib.tld
 


4.

        PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
 "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">


  1.0
  1.1
  mytaglib
  /mytaglib

 
    hello
    mypack.HelloTag
    empty
    Just Says Hello
 



◎编译servlet需要在classpath中添加servlet-api.jar
jidi查找datasource的方法:
流程Context.lookup()->datasource.getConnection()->connection->preparestatement()
并且要根据一下步骤进行必要的配置:
1.配置server.xml
2.配置web.xml
->1.

reloadable="true" >


                 auth="Container"
               type="javax.sql.DataSource" />

 
   
      factory
      org.apache.commons.dbcp.BasicDataSourceFactory
   

   
   
      maxActive
      100
   

   
   
      maxIdle
      30
   

   
   
      maxWait
      10000
   

   
   
     username
     dbuser
   

   
     password
     1234
   

   
   
       driverClassName
       com.mysql.jdbc.Driver
   

   
   
      url
      jdbc:mysql://localhost:3306/BookDB?autoReconnect=true
   

 


->2.
 


 
       DB Connection
       jdbc/BookDB
       javax.sql.DataSource
       Container
   

3.源文件实例:

<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<%@ page import="com.mysql.jdbc.Connection"%>

<%@ page contentType="text/html; charset=GB2312" %>


  DbJsp1.jsp


<%
//以try开始
try
{
java.sql.Connection con;
Statement stmt;
ResultSet rs;

//建立数据库连接
Context ctx = new InitialContext();
DataSource ds =(DataSource)ctx.lookup("java:comp/env/jdbc/BookDB");
con = ds.getConnection();
//创建一个JDBC声明
stmt = con.createStatement();
//增加新记录
stmt.executeUpdate("INSERT INTO books (id,name,title,price) VALUES ('999','Tom','Tomcat Bible',44.5)");
//查询记录
rs = stmt.executeQuery("SELECT id,name,title,price from books");
//输出查询结果
out.println("

");
while (rs.next())
{
String col1 = rs.getString(1);
String col2 = rs.getString(2);
String col3 = rs.getString(3);
float col4 = rs.getFloat(4);

//convert character encoding
col1=new String(col1.getBytes("ISO-8859-1"),"GB2312");
col2=new String(col2.getBytes("ISO-8859-1"),"GB2312");
col3=new String(col3.getBytes("ISO-8859-1"),"GB2312");

//打印所显示的数据
out.println("

");
}
out.println("
"+col1+""+col2+""+col3+""+col4+"
");

//删除新增加的记录
stmt.executeUpdate("DELETE FROM books WHERE id='999'");

//关闭数据库连结
rs.close();
stmt.close();
con.close();
}

//捕获错误信息
catch (Exception e) {out.println(e.getMessage());}

%>





















































































































































----------------------------------
{
◎如何设置dns,进行虚拟主机的配置






























































































































 

}

你可能感兴趣的:(communication,by,myself,Question,initial,pharse,more.)