◎使用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" %>
'-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN'
'http://java.sun.com/j2ee/dtds/web-app_2_3.dtd'>
4.
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
◎编译servlet需要在classpath中添加servlet-api.jar
jidi查找datasource的方法:
流程Context.lookup()->datasource.getConnection()->connection->preparestatement()
并且要根据一下步骤进行必要的配置:
1.配置server.xml
2.配置web.xml
->1.
type="javax.sql.DataSource" />
->2.
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" %>
//建立数据库连接
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("
"+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,进行虚拟主机的配置
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
◎
}