如何在servlet中获取td的内容

时间:2014-03-19 15:54:52

标签: java html postgresql jsp servlets

我在jsp中创建了一个表,我想在servlet中获取td的内容。 这是我想要获取内容的表的td:

<td  align="center" title="tdun" id="cat"> <%= categoryname %> </td>  

我使用隐藏的输入类型:

<td  align="center" title="tdun" id="cat"> <%= categoryname %>
                                                                                <input type="hidden" name="hiddenfield1" value="<%= categoryname %>"/></td>

这是servlet代码:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
        // TODO Auto-generated method stub  

        try {  

              Class.forName("org.postgresql.Driver");  
              System.out.println("Driver O.K.");  

              String url = "jdbc:postgresql://localhost:5432/SQLtest";  
              String user = "postgres";  
              String passwd = "DataBase";  

              Connection conn = DriverManager.getConnection(url, user, passwd);  
              System.out.println("Connexion effective !");     
              String name = request.getParameter("hiddenfield1");  
              PreparedStatement pre = conn.prepareStatement("delete from category where category_name=?");  
                pre.setString(1, name);  
                pre.executeUpdate();    
                pre.close();  
                conn.close();  


        }catch (Exception e)  
        {  

              e.printStackTrace();  
            }

出现异常:

org.postgresql.util.PSQLException: L'indice de la colonne est hors limite : 1, nombre de colonnes : 0.  
    at org.postgresql.core.v3.SimpleParameterList.bind(SimpleParameterList.java:56)  
    at org.postgresql.core.v3.SimpleParameterList.setNull(SimpleParameterList.java:134)  
    at org.postgresql.jdbc2.AbstractJdbc2Statement.setNull(AbstractJdbc2Statement.java:1213)  
    at org.postgresql.jdbc3.AbstractJdbc3Statement.setNull(AbstractJdbc3Statement.java:1490)  
    at org.postgresql.jdbc4.AbstractJdbc4Statement.setNull(AbstractJdbc4Statement.java:84)  
    at org.postgresql.jdbc2.AbstractJdbc2Statement.setObject(AbstractJdbc2Statement.java:1872)  
    at org.postgresql.jdbc3g.AbstractJdbc3gStatement.setObject(AbstractJdbc3gStatement.java:36)  
    at org.postgresql.jdbc4.AbstractJdbc4Statement.setObject(AbstractJdbc4Statement.java:47)  
    at com.pack.servlet.Delete.doGet(Delete.java:48)  
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)  
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)  
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)  
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)  
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)  
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)  
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)  
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)  
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)  
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)  
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)  
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)  
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)  
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)  
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:409)  
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1044)  
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)  
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2441)  
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2430)  
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)  
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)  
    at java.lang.Thread.run(Unknown Source)  

隐藏输入的使用是否正确? 我的代码有什么问题?

1 个答案:

答案 0 :(得分:0)

你应该像你一样包含在你的jsp文件中

<form action="ServletName" method="post">
// your codes 
<td  align="center" name="test" title="tdun" id="cat"><input type="text" name="test"   value="<%= categoryname %>"/> </td> 
</form>

现在在您的servlet中,即ServletName

String name=request.getParmeter("test");
out.println(name);
//this will print the value which you can insert into postgresql