さて、基本的に 3 つのドロップダウンを作成し、データベースから取得した情報を入力する最初の jsp ページを作成しました。
しかし、これは悪いコードであり、そのデータベース機能とエラー処理にはサーブレットを使用し、表示は厳密に jsp に任せるべきだと言われました。
元の jsp コードは次のとおりです。
<%@page import="java.sql.*"%>
<!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=ISO-8859-1">
<title>Code Selector</title>
</head>
<body>
<h1>Please select the applicable codes:</h1>
<select name='Code' onchange="showState(this.value)">
<option value="none">Select a code</option>
<%
//Pulls the ids and decriptions from the codes table and stores them in the first drop down
try
{
Class.forName("driverName").newInstance();
Connection con = DriverManager.getConnection("serverURL","username","password");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select id, descr from codes");
while(rs.next())
{
%>
<option value="<%=rs.getString(1)%>"><%=rs.getString(1)%> <%=rs.getString(2)%></option>
<%
}
//Closes the database connection
stmt.close();
con.close();
}
catch (ClassNotFoundException e)
{
System.err.println("ClassNotFoundException: " + e.getMessage());
}
catch (SQLException e)
{
System.err.println("SQLException: " + e.getMessage());
}
catch (Exception e)
{
System.err.println("Generic Exception: " + e.getMessage());
}
%>
</select>
<br>
<br>
<select name='Code2' onchange="showState(this.value)">
<option value="none">Select a code</option>
<%
//Pulls the ids and decriptions from the codes table and stores them in the second drop down
try
{
Class.forName("driverName").newInstance();
Connection con = DriverManager.getConnection("serverURL","username","password");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select id, descr from codes");
while(rs.next())
{
%>
<option value="<%=rs.getString(1)%>"><%=rs.getString(1)%> <%=rs.getString(2)%></option>
<%
}
//Closes the database connection
stmt.close();
con.close();
}
catch (ClassNotFoundException e)
{
System.err.println("ClassNotFoundException: " + e.getMessage());
}
catch (SQLException e)
{
System.err.println("SQLException: " + e.getMessage());
}
catch (Exception e)
{
System.err.println("Generic Exception: " + e.getMessage());
}
%>
</select>
<br>
<br>
<select name='otherCode' onchange="showState(this.value)">
<option value="none">Select a other code</option>
<%
//Pulls the ids and decriptions from the other codes table and stores them in the third drop down
try
{
Class.forName("driverName").newInstance();
Connection con = DriverManager.getConnection("serverURL","username","password");
Statement stmt = con.createStatement();
ResultSet rs2 = stmt.executeQuery("select id, descr from other_codes");
while(rs2.next())
{
%>
<option value="<%=rs2.getString(1)%>"><%=rs2.getString(1)%> <%=rs2.getString(2)%></option>
<%
}
//Closes the database connection
stmt.close();
con.close();
}
catch (ClassNotFoundException e)
{
System.err.println("ClassNotFoundException: " + e.getMessage());
}
catch (SQLException e)
{
System.err.println("SQLException: " + e.getMessage());
}
catch (Exception e)
{
System.err.println("Generic Exception: " + e.getMessage());
}
%>
</select>
<br>
<br>
<form method = "post">
<input type="submit" value="Submit">
<%
try
{
String Code = request.getParameter("Code");
String Code2 = request.getParameter("Code2");
String otherCode = request.getParameter("otherCode");
Class.forName("driverName").newInstance();
Connection con = DriverManager.getConnection("serverURL","username","password");
Statement stmt = con.createStatement();
//ResultSet rs3 = stmt.executeQuery();
System.out.println("This is the first code: " + Code);
System.out.println("This is the second code: " + Code2);
System.out.println("This is the other code: " + otherCode);
con.close();
stmt.close();
}
catch (ClassNotFoundException e)
{
System.err.println("ClassNotFoundException: " + e.getMessage());
}
catch (SQLException e)
{
System.err.println("SQLException: " + e.getMessage());
}
catch (Exception e)
{
System.err.println("Generic Exception: " + e.getMessage());
}
%>
<script>
window.close();
</script>
</form>
</body>
</html>
これまでのところ、新しい jsp とサーブレットのページは次のようになっています。
コードセレクター.jsp
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<TITLE>
Codes
</TITLE>
</HEAD>
<BODY BGCOLOR="#FDF5E6">
<H2 ALIGN="CENTER">
Please select the applicable codes:
</H2>
<FORM ACTION="http://localhost:8088/SomeProgram" METHOD="GET">
<CENTER>
<select name='code' onchange="showState(this.value)">
<option value="none">Select a code</option>
</select>
<BR>
<BR>
<select name='code2' onchange="showState(this.value)">
<option value="none">Select a code</option>
</select>
<BR>
<BR>
<select name='otherCode' onchange="showState(this.value)">
<option value="none">Select an other code</option>
</select>
<BR>
<BR>
<!-- Press this to submit form -->
<INPUT TYPE="SUBMIT" VALUE="Submit"/>
</CENTER>
</FORM>
</BODY>
</HTML>
PullCodes.java (サーブレット) :
package com.firstservlet.alfresco;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
/**
* Servlet implementation class PullCodes
*/
@WebServlet("/PullCodes")
public class PullCodes extends HttpServlet
{
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public PullCodes()
{
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String code = request.getParameter("code");
String code2 = request.getParameter("code2");
String otherCode = request.getParameter("otherCode");
try
{
Class.forName("driverName").newInstance();
Connection con = DriverManager.getConnection("url","username","password");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select id, descr from ref_codes");
ResultSet rs2 = stmt.executeQuery("select id, descr from ref_other_codes");
try
{
while(rs.next())
{
//Is this correct?
code+=("<option value=\"" + rs.getString(1) + "\">" + rs.getString(1) + " " + rs.getString(2) + "</option>");
}
//Closes the database connection
stmt.close();
con.close();
}
catch (Exception e)
{
System.err.println("Insertion Exception: " + e.getMessage());
}
}
catch (ClassNotFoundException e)
{
System.err.println("ClassNotFoundException: " + e.getMessage());
}
catch (SQLException e)
{
System.err.println("SQLException: " + e.getMessage());
}
catch (Exception e)
{
System.err.println("Generic Exception: " + e.getMessage());
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
// TODO Auto-generated method stub
}
}
そのため、現時点では、HTML ページからそのドロップダウン リスト要素にアクセスする方法がわかりません。そして、グーグルで調べてみると、これが行われていることを示すようなものは何も見られませんでした. また、私が読んだところによると、ほとんどの場合、サーブレットは html/jsp ページからの情報を処理しているだけのようです。私が今していることはcode+=("<option value=\"" + rs.getString(1) + "\">" + rs.getString(1) + " " + rs.getString(2) + "</option>");
. あれは正しいですか?もしそうなら、それをhtml/jspページにリンクするにはどうすればよいですか? それとも、ロード時にその html ページにアクセスして、サーブレットを使用してデータを入力することさえできないのでしょうか?