gpt4 book ai didi

java - JSP 未显示结果

转载 作者:行者123 更新时间:2023-12-01 09:26:00 26 4
gpt4 key购买 nike

我的 Java servlet 和应用程序出现问题。我正在绞尽脑汁地试图了解如何使这些 servlet 正常工作并在浏览器中输出。我在下面附上了我的代码,请为我指出使该程序正常工作的方向。我理解示例代码与被告知要做什么。我不是找你做我的工作,只是需要视觉上的帮助。非常感谢所有帮助。

Servlet

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class FormPost3 extends HttpServlet{
private static final long serialVersionUID = 1L;
Connection con = null;

public void Form3(){
init();
}

public void init(){
try{
Class.forName ("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:7070:XE", "student2", "pass");
Statement stmt = con.createStatement();
stmt.executeUpdate("CREATE TABLE MYTABLE (FNAME VARCHAR2(20),LNAME VARCHAR2(40), PHONE VARCHAR2(20))");
stmt.close();
}
catch (Exception e){
}
}

public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<form action='" + request.getRequestURL() + "' method='post' >");
out.println("First Name:");
out.println("<input type='text' name='FNAME' />");
out.println("<br>");

out.println("Last Name:");
out.println("<input type='text' name='LNAME' />");
out.println("<br>");

out.println("Phone:");
out.println("<input type='text' name='PHONE' />");
out.println("<input type='submit' value='Submit' />");
out.println("</form>");

out.println("</body></html>");
out.close();
}

public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
try{
if (con != null)
init();
String fname = request.getParameter("FNAME");
String lname = request.getParameter("LNAME");
String phone = request.getParameter("PHONE");
Statement stmt = con.createStatement();
stmt.executeUpdate("INSERT INTO MYTABLE VALUES('" + fname + "', '" + lname + "', '" + phone + "')");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body>");
ResultSet rset = stmt.executeQuery("SELECT * FROM MYTABLE");
while (rset.next()){
out.print("<pre>");
out.print("First Name: " + rset.getString(1));
out.print("</pre>");
out.println();
out.print("<pre>");
out.print("Last Name: " + rset.getString(2));
out.print("</pre>");
out.println();
out.print("<pre>");
out.print("Phone: " + rset.getString(3));
out.print("</pre><br>");
out.println();
out.println();
}
out.println("</body></html>");
out.close();
stmt.close();
}
catch (Exception e){
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println(e.getMessage());
out.println("</body></html>");
out.close();
}
}
}

Web.xml

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<session-config>

<session-timeout>

30

</session-timeout>

</session-config>

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

<servlet>

<servlet-name>FormPost3</servlet-name>

<servlet-class>FormPost3</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>FormPost3</servlet-name>

<url-pattern>/FormPost3</url-pattern>

</servlet-mapping>

</web-app>

index.html

<html>

<head>

</head>

<body>

<h3 align="center">Storing Employee Details</h3>

<form action="/WFormPost3" method="post">

<div align="center">

Empno: <input type="text" size="20" name="empno"/>

<br><br>

Ename <input type="text" size="20" name="ename"/>

<br><br>

Salary <input type="text" size="20" name="sal"/>

<br><br>

<input type="submit" value="insert" />

<div>

</form>

</body>

</html>

最佳答案

将index.html中的表单操作更改为
<form action="WFormPost3" method="post">

通过调试确保您的 doPost 中不存在任何异常方法WFormPost3.java servlet。

关于java - JSP 未显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39822099/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com