我希望能够单击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。在我的参考图书馆。还有其他地方需要吗?
答案 0 :(得分:11)
根据您自己的屏幕截图,您引用了sqljdbc.jar
和sqljdbc4.jar
。您应该只引用一个(在这种情况下:sqljdbc4.jar
)。
这些jar文件包含相同的驱动程序类(尽管确切的实现可能有所不同)。类加载器从类路径的第一个jar加载类,并忽略第二个jar中的类,因为它已经加载了一个具有相同名称的类。