一.工具軟體名稱:
MyEclipse 8.0 +jboss-4.0.5.G
二.準備工作:
解壓jboss,然後設定系統Path,%JBOSS_HOME%/bin;
三.在MyEclipse8裡配置JBOSS伺服器
在MyEclipse8下的Window-->Preferences下的Servers找到JBoss下的JBoss4.x
點OK後
這個小三角下就有一個JBOSS4.X,點Start運作伺服器。
四.使用Hypersonic 資料庫建立一個表格的具體步驟:
步驟一:測試JBoss伺服器是否正常運作,在IE浏覽器中輸入網址:http://127.0.0.1:8080/jmx-console/
步驟二:在該網頁中查找一個叫做“jboss”子标題,其下有個超級連結,該超級連結文字是
“database=localDB,service=Hypersonic”
步驟三:在該頁面中有一個叫做“startDatabaseManager”的MBean 操作,在它的右邊有個“Invoke”
步驟四:點選“Invoke”按鈕則會彈出一個名為“HSQL Database Manager”的資料庫管理程式。左邊列舉了目前資料庫中已經存在的表格和資料庫的名稱,右邊是一個用于輸入SQL語句的文本框和一個顯示結果的文本框。
步驟五:在“HSQL Database Manager”的資料庫管理程式中建立兩個表格(用于“聯網心理測試”),
其中表格“STOREANSWER”包括3個字段:使用者,ID,使用者姓名和測試答案,這個表格存儲用戶端的一些基本資訊和測試答案。
表格“IQQUESTION” 包括3個字段:問題序号,問題内容,問題的标題答案,這個表格負責提供測試的題目和标準答案,
建立表格的代碼如下:
CREATE TABLE STOREANSWER (USERID VARCHAR(6) NOT NULL PRIMARY KEY,USERNAME VARCHAR(15),ANSWER VARCHAR(30))
CREATE TABLE IQQUESTION (ANSEWRID VARCHAR(6) NOT NULL PRIMARY KEY,QUESTION VARCHAR(500),ANSWER VARCHAR(2))
在工具欄單擊“Execute SQL Statement”
步驟六:在表格“IQQUESTION”中填入一些測試資料,代碼如下:
INSERT INTO IQQUESTION (ANSWERID , QUESTION , ANSWER) VALUES ('Q1','Question1','A')
INSERT INTO IQQUESTION (ANSWERID , QUESTION , ANSWER) VALUES ('Q2','Question2','B')
INSERT INTO IQQUESTION (ANSWERID , QUESTION , ANSWER) VALUES ('Q3','Question3','C')
INSERT INTO IQQUESTION (ANSWERID , QUESTION , ANSWER) VALUES ('Q4','Question4','D')
INSERT INTO IQQUESTION (ANSWERID , QUESTION , ANSWER) VALUES ('Q5','Question5','A')
INSERT INTO IQQUESTION (ANSWERID , QUESTION , ANSWER) VALUES ('Q6','Question6','B')
在工具欄單擊“Execute SQL Statement”
步驟七:在右邊輸入查詢語句,SELECT * FROM IQQUESTION,在工具欄單擊“Execute SQL Statement”
五:編寫測試程式Servlet
步驟一:在MyEclipse8下建立HttpServlet,file—>new—>Web Project 建個工程名為ServletTest,
步驟二:在此工程下建立一個Servlet,命名為NetWorkTest ,在file—>new—>MyEclipse—>web—>Servlet
代碼如下:
package com.test;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.naming.InitialContext;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
public class NetWorkTest extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
// 連接配接池名稱
protected static final String DBName = "java:DefaultDS";
private DataSource dataSource;
private String question;
public void init(ServletConfig config) throws ServletException {
try {
// 初始化連接配接池
InitialContext ic = new InitialContext();
dataSource = (DataSource) ic.lookup(DBName);
} catch (Exception e) {
e.printStackTrace();
throw new ServletException("init error");
}
}
/**
* Constructor of the object.
*/
public NetWorkTest() {
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
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String userid = null;
Connection c;
try {
c = dataSource.getConnection();
// 執行查詢語句
PreparedStatement ps = c.prepareStatement("SELECT *"
+ " FROM IQQUESTION");
ResultSet rs = ps.executeQuery();
PrintWriter out = response.getWriter();
while (rs.next()) {
// 獲得表格第一個字段的資料
question = rs.getString(2);
response.setContentType("text/plain");
// 顯示到螢幕上
out.print(this.getServletInfo() + "/n");
out.print("QUESTION:" + question + "/n");
out.print("
");
}
// out.flush();
out.close();
rs.close();
ps.close();
c.close();
} catch (Exception e) {
e.printStackTrace();
}
// response.setContentType("text/html");
// PrintWriter out = response.getWriter();
// out
// .println("");
// out.println("");
// out.println(" A Servlet");
// out.println(" ");
// out.print(" This is ");
// out.print(this.getClass());
// out.println(", using the GET method");
// out.println(" ");
// out.println("");
}
/**
* 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
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
// response.setContentType("text/html");
// PrintWriter out = response.getWriter();
// out
// .println("");
// out.println("");
// out.println(" A Servlet");
// out.println(" ");
// out.print(" This is ");
// out.print(this.getClass());
// out.println(", using the POST method");
// out.println(" ");
// out.println("");
// out.flush();
// out.close();
}
}
步驟三:在web.xml中輸入釋出Servlet必須資訊。
代碼如下:
1: <servlet>
2: <description>This is the description of my J2EE component
description>
3: <display-name>This is the display name of my J2EE component
display-name>
4: <servlet-name>NetWorkTest
servlet-name>
5: <servlet-class>com.test.NetWorkTest
servlet-class>
6:
servlet>
7:
8: <servlet-mapping>
9: <servlet-name>NetWorkTest
servlet-name>
10: <url-pattern>/NetWorkTest
url-pattern>
11:
servlet-mapping>
注意:第5,10行一定要和上面的一樣。com.test是包名
步驟四:釋出Servlet,點MyEclipse裡
釋出按鈕,
在這個視窗裡選要釋出的項目名,ServletTest,在點add,找到jboss伺服器,
一路OK後。就釋出完了。
步驟五:啟動JBOSS
jboss –>start
六:在IE浏覽器上通路Servlet
在IE浏覽器上輸入http://127.0.0.1:8080/ServletTest/NetWorkTest
七:這時你的伺服器就配置完了。