我必须显示在index.jsp中编码的页面,在此页面中我使用了onload submit
<script type="text/javascript">
function myfunc () {
var frm = document.getElementById("my_form");
frm.submit();
}
window.onload = myfunc;
</script>
<form id="my_form" action="tempmonitor" method="post">
以上是我的jsp代码,我在这个页面上打印了你好。但当我点击URL localhost:8090 / tempmonitoringsystem / index.jsp。它显示了nanosec den的index.jsp走到了以下的URL localhost:8090 / tempmonitoringsystem / tempmonitor,我不知道它在哪里重定向到。我正在附加我的servlet代码以及xml代码。
TempMonitorServlet.java
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("hello world");
String path = "E:\\";
String files;
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++)
{
if (listOfFiles[i].isFile())
{
files = listOfFiles[i].getName();
if (files.endsWith(".txt") || files.endsWith(".TXT"))
{
System.out.println(files);
ReadingFromFile read_file = new ReadingFromFile();
String last_line_from_file = read_file.read(files) ;
System.out.println("\n"+last_line_from_file);
if(last_line_from_file != null)
{
drawImage(req,resp ,last_line_from_file,files) ;
}
}
}
}
}
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>ZigbeeRest</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
<servlet>
<servlet-name>tempmonitor</servlet-name>
<servlet-class>tempmonitor.TempMonitorServlet
</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.restful.Zigbee.services.ZigbeeApplication</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>tempmonitor</servlet-name>
<url-pattern>/tempmonitor</url-pattern>
</servlet-mapping>
答案 0 :(得分:0)
您的servlet没有发送任何响应,您的表单希望为其请求收到该响应。您需要向HttpServletResponse resp
“打印”某些内容。换句话说,
PrintWriter out = response.getWriter();
out.write("hello world");
// loop over your files,
out.write(file);
无论您写入PrintWriter的任何内容都将被发送回客户端。
我假设这是你System.out.println
的意思,并且你不是想把东西打印成日志文件或类似的东西。
答案 1 :(得分:0)
好吧,当你调用index.jsp时,页面被加载了。加载页面时,javascript将触发您的表单提交。由于您将表单操作设置为“tempmonitor”,因此页面将发送到/ tempmonitoringsystem / tempmonitor。
我不确定“/ tempmonitoringsystem”是您的webapp上下文还是您正在应用程序容器的根上下文中部署。如果是这样/ tempmonitor将不匹配为servlet。
在doPost方法中,您也不会写入输出流。