我正在使用Windows 8.1 64位操作系统,oracle 12c数据库和netbeans ide.i在我的netbeans IDE中创建了一个名为 WebApplication1 的java Web项目。 在我的数据库中,我创建了一个用户 C ## abc 和密码 abc ,数据库SID为 orcl 。在我的数据库中,我创建了一个名为 city 的表,列名为地址,并将一个数据“xyz”插入表格城市。我在文本框中编写城市后运行项目并在浏览器中收到消息 - Servlet NewServlet at / WebApplication1 但它应显示 xyz 。我哪里错了?
我添加了2个jar苍蝇ojdbc14.jar和ojdbc_14.jar文件。
我的new.jsp
<form action="NewServlet" method="post">
<font face="verdana" size="2">
Enter Table Name :<input type="text" name="table">
<input type="submit" value="Display">
</font>
</form>
NewServlet.java
package p;
import java.io.IOException;
import java.io.PrintWriter;
import static java.lang.System.out;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class NewServlet extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet NewServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet NewServlet at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String tb=request.getParameter("table");
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","c##abc","abc");
Statement st=con.createStatement();
out.println("connection established successfully...!!");
ResultSet rs=st.executeQuery("Select * from "+tb);
while(rs.next())
{
out.println(rs); }
}
catch (ClassNotFoundException | SQLException ex){
out.println(ex);
}
答案 0 :(得分:2)
在doPost()
方法中,您使用的out
为System.out
,已导入(作为静态)。在processRequest()
中,您可以使用以下内容覆盖它:
PrintWriter out = response.getWriter()
...这有点令人困惑,我会选择一个不同的变量名称。但是你没有在doPost().
中这样做,所以你用println
在该方法中写的任何东西都是通过HttpServletResponse
对浏览器做的,它只是去JVM控制台。
从您发布的代码中,processRequest()
如何被调用并不明显 - 您必须看到所获得的输出。你没有从该方法中显示对doGet()
/ processRequest()
的调用,或者servlet处理正在自动回退到doGet()
,因为响应尚未启动,或者它正在重定向在错误情况发生后自动回复。
即使使用response.getWriter()
的本地变量,您也需要发送正确的HTML,而不仅仅是现在的简单非标记消息,否则您只会得到一个空白页。