2009年7月15日 星期三

JSP+MultiPartRequest實作FileUpload

JSP要實作檔案上傳的方法很多,例如:jspSmart/O'Reilly/Jakarta等等,不過小弟找不到jspSmart的官網,在此以O'Reilly所提供的API為例:
1.先到http://www.servlets.com/cos/index.html下載套件(cos-26Dec2008.zip)
2.將ZIP檔unzip,然後將lib資料夾全都COPY到Web應用程式的WEB-INF下
3.先撰寫一個fileUpload.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>

<h1>Hello World!</h1>
<form action="implementUpLoad.jsp" method="POST" enctype="multipart/form-data">
<input type="file" name="files" value="" width="20" />
<input type="hidden" name="fileupload" value="go" /><br>
<input type="submit" value="Upload" />
</form>

</body>
</html>

注意:<form>中一定要加enctype="multipart/form-data"
4.再選寫一個implementUpLoad.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8" import="com.oreilly.servlet.MultipartRequest" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
<%
try {
String saveDirectory = "C:\\htdocs\\JSP\\jsp\\web\\files\\";
int maxPostSize = 5 * 1024 * 1024;
MultipartRequest multi = new MultipartRequest(request, saveDirectory, maxPostSize, "UTF-8");

//MultipartRequest m=new MutipartRequest();
out.println(multi.getFileNames().nextElement());
out.println(multi.getFilesystemName((String)multi.getFileNames().nextElement()));
} catch (Exception e) {
out.println(e.getMessage());
}
%>
</body>
</html>

資料來源:http://caterpillar.onlyfun.net/Gossip/JSPServlet/FileUpload.htm

2009年7月9日 星期四

JSP+JavaBeans

JavaBeans這玩意很神奇,也不錯玩,不過小弟我可也是玩了一星期才終於能使用:
1.先建立一個新的(我是建立C:\htdocs\JSP\jsp)
2.到C:\Program Files\Apache Software Foundation\Tomcat 6.0\conf(因人而異)底下有個server.xml檔,並在之間加入
註:"path"是虛擬的路徑名稱(http://localhost:8080/jsp)
 "docBase"是Web Application的原始路徑
 "reloadable"若為true,則當你的*.class有所變動時,可以不必將Tomcat重開
3.先建立JavaBeans:

//setData.java
package company;

public class setData {

private String name = null;
private int age = 0;

public setData() {
}

public void setName(String in) {
this.name = in;
}

public String getName() {
return this.name;
}

public void setAge(int age) {
this.age = age;
}

public int getAge() {
return this.age;
}
}

之後在建立setData.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
if(request.getParameter("action").equalsIgnoreCase("go")){
%>
<jsp:useBean id="setData" class="company.setData" />
<jsp:setProperty name="setData" property="name" param="name" />
<jsp:setProperty name="setData" property="age" param="age" />
<jsp:getProperty name="setData" property="name" />
<jsp:getProperty name="setData" property="age" />
<%
}
%>
<h1>Hello World!</h1>
<form action="setData.jsp" method="POST">
名字:<input type="text" name="name" value="" /><br>
年齡:<input type="text" name="age" value="" />
<input type="hidden" name="action" value="go" />
<input type="submit" value="Go" />
</form>
</body>
</html>

註:"property"的部份如果小寫不行請將其改成和所要呼叫的方法名稱同樣大小寫
4.將setData.java compiler後,把setData.class COPY到WEB-INF\classes\company底下
完成後即可用Brower進行測試,請注意,在JSP 2.0中,*.java必需要有package的指定.

2009年7月5日 星期日

簡意的WEB版SQL練習程式

看了許久,JSP終於看了一半上下了,雖然MySQL的基本應用大致上都會了,但多看一次就多一次收獲,所以就少稍為K一下,書附範例中,有附簡單的WEB版的練習介面,不過老是用別人的,自己怎麼會進步,所以我自己也寫了一個,我是用JNDI的方式去寫的:

<%@page contentType="text/html" pageEncoding="UTF-8" import="java.sql.*, javax.sql.*, javax.naming.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%! Connection con = null;
Statement stmt = null;
ResultSet rs = null;
ResultSetMetaData rsmd = null;

public void jspInit() {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}

public void jspDestory() {
try {
stmt.close();
rs.close();
con.close();
} catch (Exception e) {
}
}
%>
<% request.setCharacterEncoding("UTF-8");%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<table border="1">
<tbody>
<tr>
<td><center>[Result]</center>
<%
if (request.getParameter("client") != null) {
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb?user=root&password=111111");
stmt = con.createStatement();
rs = stmt.executeQuery(request.getParameter("client"));
rsmd = rs.getMetaData();
%>
<table border="1">
<thead>
<tr>
<%
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
%>
<th><%=rsmd.getColumnLabel(i)%></th>
<%
}
%>
</tr>
</thead>
<tbody>
<%
while (rs.next()) {
%>
<tr>
<%
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
%>
<td><%=rs.getString(i)%></td>
<%
}
%>
</tr>
<%
}
%>
</tbody>
</table>
<%
}
%>
</td>
</tr>
<tr>
<td><form action="sqlTool.jsp" method="POST">
Practice area:<br>
<textarea name="client" rows="10" cols="50"><%=request.getParameter("client")%></textarea>
<input type="submit" value="Test" name="OK" /></form></td>
</tr>
</tbody>
</table>
</body>
</html>

讓JSP連MySQL設定過程 for WIN

在執行以下動作時,請確定以可以執行JSP的網頁:
1.至MySQL的網站中找到MySQL Connectors
2.將JDBC Driver for MySQL (Connector/J)下載下來
3.將抓下來的檔案放在Tomcat/lib/底下(以我的PC為例:C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib)
4.在"環境變數"中,將該檔加入即可