标签:eal com 服务 override oracl test try AC prot
一 Servlet
html:
<a href="HttpServletTest">去后台</a>
web.xml:
<servlet>
<servlet-name> HttpServletTest</servlet-name>
<servlet-class>servlet.HttpServletTest</servlet-class> // 包名.类名
</servlet>
<servlet-mapping>
<servlet-name> HttpServletTest</servlet-name>
<url-pattern>/HttpServletTest</url-pattern> // url路径
</servlet-mapping>
servlet
public class HttpServletTest2 extends HttpServlet{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//数据库调用
DBUtil util=new DBUtil();
ResultSet rs=util.query("select * from sys_company");
try {
if(rs.next()){
String companyCode=rs.getString("company_code");
System.out.println(companyCode);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
util.closeAll();
}
//页面跳转
request.getRequestDispatcher("WEB-INF/page/login.html").forward(request, response);
//response.sendRedirect("WEB-INF/page/login.html"); //WEB-INF 是安全目录 客户端不能访问. 因此重定向不可以 要用服务器内部转发
}
}
JDBC
// 数据库驱动包放入WEB-INF下lib文件夹 ,并且Add To Build Path到项目中
public class DBUtil {
public static final String driver = "oracle.jdbc.driver.OracleDriver";
public static final String url = "jdbc:oracle:thin:@localhost:1521:orcl";
public static final String name = "travel";
public static final String pwd = "travel";
Connection con = null;// 连接对象
PreparedStatement ps = null; // 预编译语句对象
ResultSet rs = null;// 结果集对象
public Connection getCon() {
try {
Class.forName(driver);
con = DriverManager.getConnection(url, name, pwd);
} catch (Exception e) {
e.printStackTrace();
} return con;
}
标签:eal com 服务 override oracl test try AC prot
原文地址:https://www.cnblogs.com/eddywong/p/9248385.html