数据库连接配置
配置方式一:
1. 在tomcat中输入地址http://localhost:8080/admin进入管理控制台,找到service节点中的host节点,在此节点中查找需要使用jndi服务的工程,并对其配置jndi服务。
2. 在工程中只需要使用服务即可,无需进行其他配置
Action中新建一个显示文件列表信息的servlet,ShowFileAction.
Showfile.jsp是此应用的视图,
ShowFileAction中需要调用FileOpenDAO来获得具体的数据
配置方式二:
context.xml
context.xml |
xml version="1.0" encoding="UTF-8"?> <Context>
<Resource name="sqlconn" auth="Container" type="javax.sql.DataSource" driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" url="jdbc:sqlserver://192.168.12.35:1433;databasename=j1201" username="sa" password="123" maxAction="20" maxIdle="10" maxWait="10000" />
Context> |
Xml配置文件中部分关于jndi的配置
<resource-ref> <description>jndidescription> <res-ref-name>sqlconnres-ref-name> <res-type>javax.sql.DataSourceres-type> <res-auth>Containerres-auth> resource-ref> |
DataSourceFactory.java
DataSourceFactory.java |
package com.ibm.factory;
import javax.naming.Context; import javax.naming.InitialContext; import javax.sql.DataSource;
public class DataSourceFactory {
private static DataSource ds = null;
public static DataSource getDataSource(){ try{
Context context = new InitialContext(); //第一步:找到Java服务器中的系统环境变量 Context jceContext=(Context)context.lookup("java:comp/env"); //第二步“从系统环境变量中 搜寻JNDI ds=(DataSource)jceContext.lookup("sqlconn");
if(ds!=null){ System.out.println("从web服务器上找到数据源了"); }
}catch (Exception e){ e.printStackTrace(); } return ds;
}
} |
FileOpenDAO.java
FileOpenDAO.java |
package com.ibm.dao;
import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import javax.sql.DataSource; import com.ibm.dto.UFile; import com.ibm.factory.DataSourceFactory;
/** * 文件表操作 * @author 123 * */ public class FileOpenDAO {
public List DataSource ds=null; Connection conn=null; Statement stmt=null; ResultSet rs=null; List try { //获得数据源 ds=DataSourceFactory.getDataSource(); conn=ds.getConnection(); stmt=conn.createStatement(); rs=stmt.executeQuery("select * from filetb");
list=new ArrayList //循环读取数据 并封装到list中 while(rs.next()){ UFile file=new UFile(); file.setFileId(rs.getInt("fileid")); file.setFiName(rs.getString("finame")); list.add(file); } } catch (Exception e) { e.printStackTrace(); }finally{ try{ if(conn!=null){ conn.close(); } }catch(Exception e){ e.printStackTrace(); }
} return list;
}
// public static void main(String[] args) { // FileOpenDAO ds= new FileOpenDAO(); // List // // for(UFile file:list){ // System.out.println(file.getFiName());
// System.out.println(list.get(0).getFileId());
// for (int i = 0; i < list.size(); i++) { // System.out.println(list.get(i).getFileId()+list.get(i).getFiName()); // } // } // } }
|
UFile.java
UFile.java |
package com.ibm.dto; /** * 用户文件类 * @author 123 * */ public class UFile { private int fileId; private String fiName; public int getFileId() { return fileId; } public void setFileId(int fileId) { this.fileId = fileId; } public String getFiName() { return fiName; } public void setFiName(String fiName) { this.fiName = fiName; }
}
|
ShowFileAction.java
ShowFileAction.java |
package com.ibm.action;
import java.io.IOException; import java.io.PrintWriter; import java.util.List;
import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import com.ibm.dao.FileOpenDAO; import com.ibm.dto.UFile;
public class ShowFileAction extends HttpServlet {
/** * Constructor of the object. */ public ShowFileAction() { super(); }
/** * Destruction of the servlet. */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here }
/** * The doGet method of the servlet. * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { FileOpenDAO fod = new FileOpenDAO(); List
//将文件列表信息存放在request对象中 request.setAttribute("filelist", list);
RequestDispatcher rd = request.getRequestDispatcher("showfile.jsp"); //服务器内部转发 将文件列表信息传递到showfile.jsp页面 rd.forward(request, response);
}
/** * The doPost method of the servlet. * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html"); PrintWriter out = response.getWriter(); out .println(""); out.println(""); out.println(" out.println(" ");out.print(" This is "); out.print(this.getClass()); out.println(", using the POST method"); out.println(" "); out.println(""); out.flush(); out.close(); }
/** * Initialization of the servlet. * * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here }
}
|
showfile.jsp
showfile.jsp |
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@page import="com.ibm.dto.UFile"%> <% List %>
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head>
<title>My JSP 'showfile.jsp' starting pagetitle>
<meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page">
head>
<body> <%for(UFile file:list){ %> <%=file.getFiName() %> <%} %> body> html>
|
上传附件功能代码
|
||
|
总配置文件
xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param> <param-name>dbconfigparam-name> <param-value>/WEB-INF/db.propertiesparam-value> context-param>
<servlet> <description>This is the description of my J2EE componentdescription> <display-name>This is the display name of my J2EE componentdisplay-name> <servlet-name>ReadConfigActionservlet-name> <servlet-class>com.ibm.action.ReadConfigActionservlet-class> <load-on-startup>1load-on-startup> servlet>
<servlet> <description>This is the description of my J2EE componentdescription> <display-name>This is the display name of my J2EE componentdisplay-name> <servlet-name>LoginActionservlet-name> <servlet-class>com.ibm.action.LoginActionservlet-class> servlet>
<servlet> <description>This is the description of my J2EE componentdescription> <display-name>This is the display name of my J2EE componentdisplay-name> <servlet-name>LogOutActionservlet-name> <servlet-class>com.ibm.action.LogOutActionservlet-class> servlet>
<servlet> <description>This is the description of my J2EE componentdescription> <display-name>This is the display name of my J2EE componentdisplay-name> <servlet-name>ShowFileActionservlet-name> <servlet-class>com.ibm.action.ShowFileActionservlet-class> servlet> <servlet> <description>This is the description of my J2EE componentdescription> <display-name>This is the display name of my J2EE componentdisplay-name> <servlet-name>UploadActionservlet-name> <servlet-class>com.ibm.action.UploadActionservlet-class> servlet>
<servlet-mapping> <servlet-name>LoginActionservlet-name> <url-pattern>/loginurl-pattern> servlet-mapping> <servlet-mapping> <servlet-name>LogOutActionservlet-name> <url-pattern>/LogOutActionurl-pattern> servlet-mapping> <servlet-mapping> <servlet-name>ReadConfigActionservlet-name> <url-pattern>/readurl-pattern> servlet-mapping> <servlet-mapping> <servlet-name>ShowFileActionservlet-name> <url-pattern>/showfileurl-pattern> servlet-mapping> <servlet-mapping> <servlet-name>UploadActionservlet-name> <url-pattern>/uploadurl-pattern> servlet-mapping>
<session-config> <session-timeout>1session-timeout> session-config>
<welcome-file-list> <welcome-file>index.jspwelcome-file> welcome-file-list>
<resource-ref> <description>jndidescription> <res-ref-name>sqlconnres-ref-name> <res-type>javax.sql.DataSourceres-type> <res-auth>Containerres-auth> resource-ref>
<login-config> <auth-method>BASICauth-method> login-config> web-app>
|