使用servlet将数据插入数据库会返回空白页

时间:2015-11-24 19:40:38

标签: java html eclipse jsp servlets

我正在尝试从html获取两个输入并将它们插入到我的数据库中。我尝试过bosth .jsp和.html文件,但没有运气。当调用jsp / html时,它返回一个白页。相反,我想提示html / jsp页面询问输入。我做错了什么???

我的日食树

enter image description here

AddInfo.java servlet

<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

 <resource-ref>
  <description>Database</description>
  <res-ref-name>testdb</res-ref-name>
  <res-type>oracle.jdbc.OracleDriver</res-type>
  <res-auth>Container</res-auth>
  </resource-ref>

  <servlet>
     <description></description>
     <display-name>AddInfo</display-name>
     <servlet-name>AddInfo</servlet-name>      
    <servlet-class>controllers.AddInfo</servlet-class>
    </servlet>
   <servlet-mapping>
    <servlet-name>AddInfo</servlet-name>
    <url-pattern>/AddInfo</url-pattern>
   </servlet-mapping>

   <welcome-file-list>
   <welcome-file>page2.html</welcome-file>
   <welcome-file>page2.htm</welcome-file>
   <welcome-file>page2.jsp</welcome-file>
   </welcome-file-list>

  </web-app>

的web.xml

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<form method="POST" id="AddInfo" action="AddInfo">
<p>
Enter name: <input type="text" name="name"><br>
Enter date: <input type="text" name="date"><br>
</p>
<br>
<p>
<input type="submit">
</p><br>
</form>

</body> 
</html>

request.html

library(ggplot2)
library(dplyr)
library(scales)

data.frame(Month = as.Date(c("2015-01-01", "2015-01-01","2015-01-01",
"2015-02-01","2015-02-01","2015-02-01")),
       types = rep(LETTERS[1:3],2),
       percent = c(0.2,NA,NA,.39,.89,.59)) %>%
  ggplot( aes(x = Month, y = percent, fill = types)) +  
  geom_bar(stat = "identity", position = "dodge") + 
  scale_y_continuous(labels = percent) +
  scale_x_date(labels = date_format("%Y-%b")) +
  geom_text(stat = 'identity', 
        aes(label = ifelse(percent > .02,
                           paste(round(percent*100,0),"%",sep = "") ,'')))

1 个答案:

答案 0 :(得分:1)

编辑:我对web.xml,request.html和servlet进行了更改。我创建了项目并对其进行了测试。试试吧。发布后续问题。

您需要将响应转发或重定向到页面(jps / html),如下所示:

       String nextJSP = "/index.html";
       request.getRequestDispatcher(nextJSP).forward(request, response);

重定向:

  private String LOGIN_PAGE = "request.html";
  // other code here ...
  response.sendRedirect(LOGIN_PAGE);

要么在post方法结束时进行,取决于你转发的原因和页面也是如此,例如你最好将一个成功的场景转发到你认为合适的页面。

编辑 - &gt;对html页面的更改 request.html:首先重命名为index.html。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<form method="POST" id="identifier" action="AddInfo">
<input type="text" id="Id" name="Id">
<input type="submit" id="sub" name="button"/>
</form>

</body> 
</html>

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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

 <resource-ref>
  <description>Database</description>
  <res-ref-name>testdb</res-ref-name>
  <res-type>oracle.jdbc.OracleDriver</res-type>
  <res-auth>Container</res-auth>
  </resource-ref>



 <servlet>
<description></description>
<display-name>CaseDeleteServlet</display-name>
<servlet-name>CaseDeleteServlet</servlet-name>
   <servlet-class>servlets.CaseDeleteServlet</servlet-class>
  </servlet>

  <servlet>
     <description></description>
     <display-name>AddInfo</display-name>
     <servlet-name>AddInfo</servlet-name>      
    <servlet-class>controllers.AddInfo</servlet-class>
    </servlet>
   <servlet-mapping>
    <servlet-name>AddInfo</servlet-name>
    <url-pattern>/AddInfo</url-pattern>
   </servlet-mapping>


   <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>
   </welcome-file-list>

  </web-app>