我正在尝试将Java类转换为在网页上显示。使用最高回答here作为指导。如果我使用System.out
打印出来的东西,Java会做它应该做的事情。当尝试转发到jsp页面时,它会循环(重新实例化?),并且不会停止(必须手动终止进程)。
Connector.java
public class Connector extends HttpServlet {
private static URL url = https://my.server.com/WEB-INF/ws/service.php?wsdl");;
private static final String USERNAME = "JoeBoB";
private static final String PASSWORD = "1337pass";
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//login to my.server.com
try {
authToken = serverAPI.login(USERNAME, PASSWORD);
System.out.println("Logged into server with Token: " + authToken);
//this shows up in console over and over again, until manually killed
}
catch (Exception ex) {
ex.printStackTrace();
}
request.setAttribute("message","bacon");
request.getRequestDispatcher("/WEB-INF/draw.jsp").forward(request, response);
//line above appears to be the one that re-inits the class.
//commenting this out stops the looping
//but also prevents the data from showing on the webpage
serverAPI.logout(authToken);
WEB-INF / draw.jsp
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>This is my drawn page</title>
</head>
<body>
${message}
</body>
</html>
WEB-INF / web.xml中
<web-app>
<display-name>Connector</display-name>
<servlet>
<servlet-name>Connector</servlet-name>
<servlet-class>com.company.package.Connector</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Connector</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
我感觉这有点简单(我忘了配置或配置错误),但是对于我的生活无法弄清楚是什么。
答案 0 :(得分:2)
通过将/ *映射到您的servlet,您将覆盖JSP请求的默认处理程序。您需要使用更具体的模式,使用文件扩展名或子目录。