2012-12-19 41 views
-1

的動作我都必須表現的index.jsp,該頁面編碼的頁面我已經使用在onload提交沒有得到我想要的jsp頁面。只有獲得URL這是在JSP

<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本地主機: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> 

+1

嗨塔倫,你確實有一個window.onload調用你的表單提交權?表單的「action」屬性必須設置爲它重定向到的servlet。 – dinukadev

+0

如果您將在web.xml中看到我已經完成了相應servlet的映射TempMonitorServlet是我所在的servlet名稱。 –

+1

請按原樣編輯問題的名稱,這是毫無意義的。 – Gangnus

回答

0

你的servlet不發送任何響應,表單希望接收˚F或其請求。你需要「打印」一些東西給你的HttpServletResponse resp。換句話說,

PrintWriter out = response.getWriter(); 

out.write("hello world"); 

// loop over your files, 
out.write(file); 

無論你寫給PrintWriter什麼東西都會發送回客戶端。

我假設這就是你的意思是System.out.println,而且你並沒有試圖將東西打印到日誌文件或類似的東西中。

+0

我使用system.out.println()僅用於調試目的。 –

0

好吧,當你調用index.jsp頁面被加載。當頁面加載時,JavaScript會觸發提交表單。由於您將表單操作設置爲「tempmonitor」,因此該頁面將發送到/ tempmonitoringsystem/tempmonitor。

我不知道「/ tempmonitoringsystem」是您的web應用程序上下文還是您在應用程序容器的根上下文中部署。如果這樣/ tempmonitor不會像servlet一樣匹配。

在您的doPost方法中,您也不會寫入輸出流。

+0

感謝您的幫助我tempmonitoringsystem是我的webapp名稱。我正在寫system.out.println(),因爲我正在檢查它是否輸入dopost或不。我只是將它用於調試目的而已。 –