码迷,mamicode.com
首页 > 其他好文 > 详细

关于项目搭建-SERVLET

时间:2018-06-30 19:58:30      阅读:151      评论:0      收藏:0      [点我收藏+]

标签: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;

 }

关于项目搭建-SERVLET

标签:eal   com   服务   override   oracl   test   try   AC   prot   

原文地址:https://www.cnblogs.com/eddywong/p/9248385.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!