JSP Form用Servlet获取客户反馈

时间:2014-11-09 16:34:24

标签: java jsp tomcat servlets

我使用Netbeans开发了一个基本表单来获取客户意见。我有一个index.jsp文件,它包含所有的html代码,一个从表单获取值的servlet和一个用于建立数据库连接并填充数据库的java类。我在tomcat服务器上运行它,但是我收到HTTP 500错误。

这是index.jsp



<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Feedback Form for Personal Account Users</title>
    </head>
    <body>
        <form name = "FeedbackForm" action="SubmitUser" method ="post">
            

            
            <p id="Questions">Your Comments:	</p>
            <textarea id="selection" rows="4" cols="100" name="comment">
            </textarea>
            <br>
            <br>    

            <input type = "submit" name ="sub" value="Submit">

        </form>
    </body>
</html>
&#13;
&#13;
&#13;

具有数据库连接的Java类

package feedback;

package feedback;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DbConnection {

private String driver = "com.mysql.jdbc.Driver";
private String url = "jdbc:mysql://localhost:3306/online_feedback";
private Connection con;
private Statement st;
private int i;


public void connectDB(){
    try {
        Class.forName(driver);
        con = DriverManager.getConnection(url, "root", "");

    } catch (ClassNotFoundException ex) {
        Logger.getLogger(DbConnection.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(DbConnection.class.getName()).log(Level.SEVERE, null, ex);
    }

}

public int insertValues(String sql){
    try {
        connectDB();
        Statement st = con.createStatement();
        i = st.executeUpdate(sql);

        return i;
    } catch (SQLException ex) {
        Logger.getLogger(DbConnection.class.getName()).log(Level.SEVERE, null, ex);
    }
    return i;
}

public void main (String []args){

    DbConnection db = new DbConnection();
    db.connectDB();


}

}

以下是Servlet(SubmitUser)从表单

获取值
package feedback;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class SubmitUser extends HttpServlet {

    private DbConnection db = new DbConnection();

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();


    String comment = request.getParameter("comment");


    String sql = "INSERT INTO test values('comment' )";

    int i = db.insertValues(sql);

    if (i==0){
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet SubmitUser</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>You have missed</h1>");
        out.println("</body>");
        out.println("</html>");
    }

    else{
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet SubmitUser</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Successfully Submmited</h1>");
        out.println("</body>");
        out.println("</html>");


    }


    }
}


@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 {
    processRequest(request, response);
}

/**
 * Returns a short description of the servlet.
 *
 * @return a String containing servlet description
 */
@Override
public String getServletInfo() {
    return "Short description";
}// </editor-fold>

}

0 个答案:

没有答案