HTMLボタンをクリックして、Java内のメソッドを呼び出せるようにしたい。このタスクを実行する方法は複数ありますか?
また、この例でエラーがどこから来ているのかを確認したいと思います。コンパイルされません。
Javaコード:
import java.io.IOException;
@WebServlet("/myservlet")
public class MyServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
MyClass myClass = new MyClass();
if (request.getParameter("button1") != null) {
myClass.function1();
} else if (request.getParameter("button2") != null) {
myClass.function2();
} else if (request.getParameter("button3") != null) {
myClass.function3();
}
request.getRequestDispatcher("/WEB-INF/some-result.jsp").forward(request, response);
}
}
class MyClass {
void function1() {
System.out.println("Button 1");
}
void function2() {
System.out.println("Button 2");
}
void function3() {
System.out.println("Button 3");
}
}
htmlコード:
<html>
<head>
<title>Test Button</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/myservlet" method="post">
<input type="submit" name="button1" value="Button 1" />
<input type="submit" name="button2" value="Button 2" />
<input type="submit" name="button3" value="Button 3" />
</form>
</body>
</html>
エラー:
May 30, 2014 1:16:54 PM com.microsoft.sqlserver.jdbc.SQLServerConnection <init>
SEVERE: Java Runtime Environment (JRE) version 1.7 is not supported by this driver. Use the sqljdbc4.jar class library, which provides support for JDBC 4.0.
Exception in thread "main" java.lang.UnsupportedOperationException: Java Runtime Environment (JRE) version 1.7 is not supported by this driver. Use the sqljdbc4.jar class library, which provides support for JDBC 4.0.
at com.microsoft.sqlserver.jdbc.SQLServerConnection.<init>(SQLServerConnection.java:304)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1011)
at java.sql.DriverManager.getConnection(DriverManager.java:571)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at testpak.DbTest.testQuery(DbTest.java:19)
at testpak.DbTest.main(DbTest.java:30)
そのsqljdbc4jar。私の参照ライブラリにあります。それが必要な別の場所はありますか?
あなた自身のスクリーンショットによると、あなたはsqljdbc.jar
との両方を参照していsqljdbc4.jar
ます。1つだけを参照する必要があります(この場合は:) sqljdbc4.jar
。
これらのjarファイルには同じドライバークラスが含まれています(ただし、正確な実装は異なる場合があります)。クラスローダーは、クラスパスの最初のjarからクラスをロードし、同じ名前のクラスを既にロードしているため、2番目のjarのクラスを無視します。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加