0

私は最近、Java を使用した Web サイトのプログラミングという素晴らしい世界に放り込まれました。私はJavaの経験が少しありますが、まだ初心者だと思っています。

単純な SQL クエリを含む Java クラスを作成しました。JSP ページに表示しようとしていますが、これを実現する方法がわかりません。

Main.java と呼ばれる私の Java クラスは次のとおりです。

public static void main(String[] args) throws Exception { 
    //Accessing driver from JAR
    Class.forName("com.mysql.jdbc.Driver");

    //Creating a variable for the connection called "con"
    //jdbc:mysql://host_name:port/dbname
    //Driver name = com.mysql.jdbc.Driver
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/wae","root","");

    PreparedStatement statement = con.prepareStatement("select name from user");
    ResultSet result = statement.executeQuery();
    while(result.next()) {
        System.out.println(result.getString(1));
    }
}

system.out.printlnJSP ページにアクセスするにはどうすればよいですか?

4

6 に答える 6

1

結果を保存して JSP に表示する必要がある場合。

結果をリクエストに保存し、ビューレイヤーでJSTLを使用して繰り返し結果を表示します。つまり、サーブレットでリクエストを取得し、それを新しいjspに転送します

JSP の代わりに JSF を使用することをお勧めします。

于 2012-09-28T23:17:32.420 に答える
0

mainWeb プログラミングの基本的な概念は、メソッド (またはその他)からアクションを開始しないことです。誰かが JSP またはサーブレットを要求し、対応するクラスが応答します。@ chaitanya10の回答が示すように、JSPで両方を実行し、データベースに接続する作業を補助クラスに任せることができます。

JSP のNebeansチュートリアルは、悪い出発点ではありません。Web には JSP/サーブレットのチュートリアルがいくつかありますが、Java EE のチュートリアルには近づかないことをお勧めします。

于 2012-09-28T22:47:05.473 に答える
0

投稿する例では、サーブレットをプログラムする必要があります。そのサーブレットでは、ビジネス ロジック呼び出し (この場合はデータベース クエリ) を行います。データを取得した後、それを JSP ページに渡す必要があります。 . サーブレットでは main メソッドは必要ありませんが、サーブレット コンテナー (Tomcat など) にデプロイする必要があります。

コードは次のとおりです。

public class UserListServlet extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
                                    throws ServletException, IOException {  
    //Accessing driver from JAR
    Class.forName("com.mysql.jdbc.Driver");

    //Creating a variable for the connection called "con"
    //jdbc:mysql://host_name:port/dbname
    //Driver name = com.mysql.jdbc.Driver
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/wae","root","");

    PreparedStatement statement = con.prepareStatement("select name from user");
    ResultSet result = statement.executeQuery();
    //creates a list with the user names
    List<String> userList = new ArrayList<String>();
    while(result.next()) {
        userList.add(result.getString(1));
    }
    //passing the data to the JSP
    request.setAttribute("users", userList);
    getServletContext().getRequestDispatcher("/user_list.jsp").forward(request, response);  
}  

protected void doGet(HttpServletRequest request, HttpServletResponse response)                           throws ServletException, IOException {  
    processRequest(request, response); 
}  

protected void doPost(HttpServletRequest request, HttpServletResponse response) 
                                throws ServletException, IOException {
    processRequest(request, response);
}

}

JSP では、次のようなものを作成できます。

<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.List" %>
<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>User List</title>
    </head>
    <body>
        <%
        if (request.getAttribute("userList") != null) {
            List<String> users = request.getAttribute("userList");
        %>

        <h1>Users: </h1>
        <table>
            <tr>
                    <td>Name<</td>
            </tr>

            <% for (String name : users) {%>

            <tr>
                <td><%= name%></td>
            </tr>
            <% }
          }%>

        </table>
    </body>
</html>

スクリプトレット (<% ... %>) の代わりにJSTLを使用すると、この例を改善できます。

于 2012-09-29T05:24:26.940 に答える
0

以下のサンプルコードを参考にしてください。

JSP page
       <form action="servlet1" method="get">
        <%   ModelClass class = new ModelClass();
        class.connectDb();
        class.performDBoperations();
          %>
       <table><tr><td>
      <%   
              class.id;     
       %> 
       </form>

Model Class:
        class ModelClass {
          int id;
            public static void connectDb() {
               dbConnection code
               }
             public void performDBoperations() {
                  get info from table with SQL thru JDBC.
                    id=update it;  
               }
于 2012-09-28T22:33:57.587 に答える
0

JSP では、JSP 内に Java コードのブロックを記述することもできます。これを行うには、Java コードを <% と %> 文字の間に配置します。スクリプトレットには、JSP が呼び出されるたびに実行される Java コードが含まれています。
これは、開始するのに役立つ単純な jsp Web アプリケーションへのリンクです。

http://j2ee.masslight.com/Chapter1.html#mygreeting

于 2012-09-28T22:21:46.457 に答える
-2

Java Web サイトを作成するのにこれが良い方法であると確信していますか?

Java EE、サーブレットについては Google に問い合わせてください。

于 2012-09-28T22:16:30.810 に答える