MyEclipse+Jboss+HSQL开发Servlet服务器

一.工具软件名称:

MyEclipse 8.0 +jboss-4.0.5.G

二.准备工作:

解压jboss,然后设置系统Path,%JBOSS_HOME%/bin;

三.在MyEclipse8里配置JBOSS服务器

在MyEclipse8下的Window-->Preferences下的Servers找到JBoss下的JBoss4.ximage

点OK后image 这个小三角下就有一个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语句的文本框和一个显示结果的文本框。

image

步骤五:在“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”

image

五:编写测试程序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(""-//W3C//DTD HTML 4.01 Transitional//EN/">");
        // 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(""-//W3C//DTD HTML 4.01 Transitional//EN/">");
        // 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必须信息。

image

代码如下:

 1:  servlet>
 2:      description>This is the description of my J2EE componentdescription>
 3:      display-name>This is the display name of my J2EE componentdisplay-name>
 4:      servlet-name>NetWorkTestservlet-name>
 5:      servlet-class>com.test.NetWorkTestservlet-class>
 6:  servlet>
 7:  
 8:  servlet-mapping>
 9:      servlet-name>NetWorkTestservlet-name>
10:      url-pattern>/NetWorkTesturl-pattern>
11:  servlet-mapping>

注意:第5,10行一定要和上面的一样。com.test是包名

步骤四:发布Servlet,点MyEclipse里image 发布按钮,

image 在这个窗口里选要发布的项目名,ServletTest,在点add,找到jboss服务器,image 一路OK后。就发布完了。

步骤五:启动JBOSSimage jboss –>start

六:在IE浏览器上访问Servlet

在IE浏览器上输入http://127.0.0.1:8080/ServletTest/NetWorkTest

image

七:这时你的服务器就配置完了。

你可能感兴趣的:(MyEclipse)