我希望能够单击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
。您只能引用一个(在这种情况下:)sqljdbc4.jar
。
这些jar文件包含相同的驱动程序类(尽管确切的实现可能有所不同)。classloader从第一个jar加载类路径中的类,而忽略第二个jar中的类,因为它已经加载了具有相同名称的类。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句