在NetBaen里新建一个Web项目,打开配置文件context.xml,输入以下内容:
<?xml version="1.0" encoding="UTF-8"?>
<Context crossContext="true" reloadable="true" path="/nutz_Text">
<Resource auth="Container" driverClassName="com.mysql.jdbc.Driver" maxActive="50" maxWait="5000" maxldle="10" name= 自定义出一个变量名 password= 数据库密码 username= 数据库管理员 type="javax.sql.DataSource" url="jdbc:mysql://localhost:3306/bat?autoReconnect=true" />
</Context>
注: <Context crossContext="true" reloadable="true" path="/nutz_Text"> 里不要有antiJARLocking="true",因为antiJARLocking="true"会有一些副作用,包括屏蔽了JSP文件在运行服务器上的重新加载。
Ok,编写测试程序:
package com;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.*;
//import java.sql.*;
import javax.naming.*;
import org.nutz.Nutz;
import org.nutz.dao.Dao;
import org.nutz.dao.impl.NutDao;
/**
*
* @author Administrator
*/
public class NewServlet extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.print("jiang");
Context context = new InitialContext();
DataSource source = (DataSource)context.lookup("java:comp/env/ 自定义的变量名");
out.print("jiang");
Dao dao =new NutDao(source); //Nutz.dao
out.print("jiang");
text p = new text();
out.print("jiang");
//p.setAge(20);
//p.setName("ABC");
dao.delete(text.class,"ABC");//根据名称删除
// out.print("删除成功: "+p.getName()+"\n");
java.util.List<text> list = dao.query(text.class, null, null);
java.util.Iterator<text> it = list.iterator();
while(it.hasNext()){
text tmp =(text)it.next();
out.print(tmp.getId()+" "+tmp.getName()+" "+tmp.getAge());
out.print("\n");
}
} catch (NamingException ex) {
//Logger.getLogger(NewServlet.class.getName()).log(Level.SEVERE, null, ex);
out.print("出错");
} finally {
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
启动Tomcat,运行这个Servlet,搞定.....
<?xml version="1.0" encoding="UTF-8"?>
<Context crossContext="true" reloadable="true" path="/nutz_Text">
<Resource auth="Container" driverClassName="com.mysql.jdbc.Driver" maxActive="50" maxWait="5000" maxldle="10" name= 自定义出一个变量名 password= 数据库密码 username= 数据库管理员 type="javax.sql.DataSource" url="jdbc:mysql://localhost:3306/bat?autoReconnect=true" />
</Context>
注: <Context crossContext="true" reloadable="true" path="/nutz_Text"> 里不要有antiJARLocking="true",因为antiJARLocking="true"会有一些副作用,包括屏蔽了JSP文件在运行服务器上的重新加载。
Ok,编写测试程序:
package com;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.*;
//import java.sql.*;
import javax.naming.*;
import org.nutz.Nutz;
import org.nutz.dao.Dao;
import org.nutz.dao.impl.NutDao;
/**
*
* @author Administrator
*/
public class NewServlet extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.print("jiang");
Context context = new InitialContext();
DataSource source = (DataSource)context.lookup("java:comp/env/ 自定义的变量名");
out.print("jiang");
Dao dao =new NutDao(source); //Nutz.dao
out.print("jiang");
text p = new text();
out.print("jiang");
//p.setAge(20);
//p.setName("ABC");
dao.delete(text.class,"ABC");//根据名称删除
// out.print("删除成功: "+p.getName()+"\n");
java.util.List<text> list = dao.query(text.class, null, null);
java.util.Iterator<text> it = list.iterator();
while(it.hasNext()){
text tmp =(text)it.next();
out.print(tmp.getId()+" "+tmp.getName()+" "+tmp.getAge());
out.print("\n");
}
} catch (NamingException ex) {
//Logger.getLogger(NewServlet.class.getName()).log(Level.SEVERE, null, ex);
out.print("出错");
} finally {
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
启动Tomcat,运行这个Servlet,搞定.....