Servlet提供空白页面

时间:2014-11-16 05:39:16

标签: java jsp servlets

我有一个Servlet需要帮助添加一个教师到我的迷你项目

但Servlet是空白的
这是Servlet:

@WebServlet(urlPatterns = {"/AddInstructorServlet"})
public class AddInstructorServlet 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
     * @throws SQLException
     */
     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */



          int iD = Integer.parseInt(request.getParameter("iD"));
          String firstName = request.getParameter("firstName");
          String lastName = request.getParameter("lastName");
          String street = request.getParameter("street");
          String city = request.getParameter("city");
          String state = request.getParameter("state");
         double zip = Double.parseDouble(request.getParameter("zip"));
          String office = request.getParameter("office");
          String eMail = request.getParameter("eMail");
           System.out.println(iD); 

            Instructor i1 = new Instructor();

            i1.insertDB(iD, firstName, lastName, street, city, state, zip, office, eMail);

            int cn=i1.getID();
            System.out.println(cn);

            String fn=i1.getFirstName();
            System.out.println(fn);

            String ln=i1.getLastName();
            System.out.println(ln);

            String str=i1.getStreet();
            System.out.println(str);

            String ci=i1.getCity();
            System.out.println(ci);

            String st=i1.getState();
            System.out.println(st);

             zip=i1.getZip();
            System.out.println(zip);

             String off=i1.getOffice();
            System.out.println(off);

            String em=i1.getEMail();
            System.out.println(em);

            RequestDispatcher rd; 

            HttpSession ses1=request.getSession();
            ses1.setAttribute("i1", i1);

               rd= request.getRequestDispatcher("/Success.jsp");
                rd.forward(request,response);



        }
    }


    /**
     * 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 {
         try {
             processRequest(request, response);
         } catch (SQLException ex) {
             Logger.getLogger(AddInstructorServlet.class.getName()).log(Level.SEVERE, null, ex);
         }
    }

    /**
     * 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 {
         try {
             processRequest(request, response);
         } catch (SQLException ex) {
             Logger.getLogger(AddInstructorServlet.class.getName()).log(Level.SEVERE, null, ex);
         }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

这是实际添加教师的JSP:

<%@page import="Business.Instructor"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
                <style>
    h1 {

     font-family:cursive;
}

body {
    background-color: #3399CC;
}
</style>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Instructor</title>
    </head>
    <body>
        <h1>Adding Instructor</h1>
        <form method="post" action="http://localhost:8084/StudentApp/AddInstructorServlet">
              <%
            HttpSession ses1=request.getSession();
            Instructor i1= (Instructor)ses1.getAttribute("i1");


            %> 
        <p><input type="text" name="iD" value="" placeholder="ID"></p>
      <p><input type="text" name="fn" value="" placeholder="First Name"></p>
        <p><input type="text" name="ln" value="" placeholder="Last Name"></p>
         <p><input type="text" name="str" value="" placeholder="Street"></p>
        <p><input type="text" name="ci" value="" placeholder="City"></p>
        <p><input type="text" name="st" value="" placeholder="State"></p>
        <p><input type="text" name="zi" value="" placeholder="Zip"></p>
        <p><input type="text" name="off" value="" placeholder="Office"></p>
        <p><input type="text" name="em" value="" placeholder="Email"></p>
        <p class="submit"><input type="submit" name="commit" value="Go"></p>
      </form>
    </body>
</html>

这是业务对象

package Business;

import java.io.Serializable;
import java.sql.*;


public class Instructor implements Serializable {

    private int iD;
    private String firstName;
    private String lastName;
    private String street;
    private String city;
    private String state;
    private double zip;
    private String office;
    private String eMail;

    private String message;

    /**
     * No arg constructor that sets the default value of all
     * customer properties to an empty string value.
     */
    public Instructor() {

        this.iD = 0;
        this.firstName = "";
        this.lastName = "";
        this.street = "";
        this.city = "";
        this.state = "";
        this.zip = 0;
        this.office = "";
        this.eMail = "";

    }


    public void setID(int iD) {
        this.iD = iD;
    }



    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }


    public void SetLastName(String lastName) {
        this.lastName = lastName;
    }


    public void setStreet(String street) {
        this.street = street;
    }

    public void setCity(String city) {
        this.city = city;
    }

      public void setState(String state) {
        this.state = state;
    }

        public void setZip(double zip) {
        this.zip = zip;
    }
     public void setOffice(String office) {
        this.office = office;
    }
          public void setEMail (String email) {
        this.eMail = email;
    }


    public int getID() {
        return iD;
    }





    public String getFirstName() {
        return firstName;
    }


    public String getLastName() {
        return lastName;
    }


    public String getStreet() {
        return street;
    }

    public String getCity() {
        return city;
    }
    public String getState() {
        return state;
    }

    public double getZip() {
        return zip;
    }
     public String getOffice() {
        return office;
    }
      public String getEMail() {
        return eMail;
    }


    public String getMessage() {
        return this.message;
    }




    /**
     * Establishes connection with the database containing the STUDENT information 
     * @return Connection
     */
    public Connection instructorConnect() {
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        } catch (ClassNotFoundException e) {
            System.out.println("Errors: " + e);
        }

        Connection connect = null;
        try {
            connect = DriverManager.getConnection("jdbc:odbc:RegisDB");
        } catch (SQLException e) {
            System.out.println("You have Errors: " + e);
        }

        return connect;
    }

    /**
     * Takes the student Id and retrieves the information from the customers
     * data table and stores it in the properties of the customer object.
     * @param iD
     * @throws SQLException 
     */
    public void findDB(int iD)  {

        Connection con = instructorConnect();
        Statement statement = null;
        ResultSet result = null;

        String sql = "Select * From Instructors Where iD = " + iD + ";";

        try {
            statement = con.createStatement();
            result = statement.executeQuery(sql);
            while (result.next()) {
                this.iD = result.getInt("iD");

                this.firstName = result.getString("firstName");
                this.lastName = result.getString("lastName");
                this.street = result.getString("street");
                this.city = result.getString("city");
                this.state = result.getString("state");
                this.zip = result.getDouble("zip");
                this.state = result.getString("office");
                this.eMail = result.getString("eMail");

            }
            con.close();

        } catch (SQLException e) {
            System.out.println("Errors: " + e);
        } 
    }

    /**
     * Takes in the parameters of the customer database and inserts the parameters taken into 
     * the database for the addition of customers to the database.
     * @param iD
     * @param firstName
     * @param lastName
     * @param street
     * @param city
     * @param state
      * @param zip
     * @param office
     * @param email

     * @throws SQLException 
     */
    public void insertDB(int iD, String firstName, String lastName, String street, String city, String state, double zip, String office, String email) throws SQLException {
        Connection con = instructorConnect();
        Statement statement;
        ResultSet result;
        int resultVal;
        String sql = "INSERT INTO Students (iD, firstName, lastName, street, city, state, zip, office, eMail) VALUES ('" + firstName + "','" + lastName + "','" + street + "','" + city +"','" + state +"','" + zip + "','" + office + "','" + eMail + "');";

        try {
            statement = con.createStatement();
            resultVal = statement.executeUpdate(sql);

            System.out.println(resultVal);
        } catch (SQLException e) {
            System.out.println("Error: " + e);
            System.out.println(e.getStackTrace());
        } finally {
            con.close();
        }
    }

    /**
     * Takes in student id and locates the customer in the database, performs
     * an sql update and deletes the customer from the database.
     * @param iD
     * @throws SQLException 
     */
    public void deleteDB(int iD) throws SQLException {
        Connection con = instructorConnect();
        Statement statement;
        int resultVal;

        String sql = "DELETE FROM Students WHERE iD = " + iD + "';";

        try {
            statement = con.createStatement();
            resultVal = statement.executeUpdate(sql);
            System.out.println(resultVal);
        } catch (SQLException e) {
            System.out.println("Errors: " + e);
        } finally {
            con.close();
        }
    }

}

http://puu.sh/cSIhA/fd87e788f4.png (学校的假数据库哈哈) 这是视觉效果的实际数据库。

我只需要使用jsp和Servlet添加一个新的教师。

0 个答案:

没有答案