简体   繁体   English

Servlet未执行

[英]Servlet is not executing

I have a program that writes new employee information to a text file. 我有一个程序可以将新员工信息写入文本文件。 I have a project in which I must adapt said program into one that inputs that info into an Oracle database instead. 我有一个项目,在该项目中,我必须将所述程序改编成一个程序,该程序将信息输入到Oracle数据库中。 I put in the info and hit the submit button, then nothing happens, not even an error message or thrown exception. 我放入信息并单击“提交”按钮,然后什么也没有发生,甚至没有错误消息或引发的异常。 I have no idea what is wrong or where to even start looking, and I can admit that I am in a little bit over my head here. 我不知道哪里出了问题,甚至不知道从哪里开始寻找东西,我可以承认我在这里有些头疼。 I am fairly certain the problem lies with the servlet, somehow. 我可以肯定地认为问题出在servlet上。 Here is my code: 这是我的代码:

form.jsp: form.jsp:

<%@page session="false" import="java.util.Iterator"%>

<%-- Retrieve the Status bean from the Request scope --%>
<jsp:useBean id="status" scope="request" class="util.Status"/>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Project 1</title>
    </head>
    <body>
        <h1>Employee Hiring Form</h1>

        <%-- Display any errors from previous form submission. --%>
        <c:if test="${!status.isSuccessful}">
            <font color="red">There were problems processing your request:      
                <ul>
                    <c:forEach var="ex" items="${status.exceptions}">
                        <li>
                            <c:out value="${ex.message}"></c:out>
                        </li>
                    </c:forEach>        
                </ul>
            </font>    
        </c:if>

        <%-- Display the form to enter a new hire. --%>
        <form     action="C:\Users\Nelle\Documents\NetBeansProjects\EmployeeDB\src\java\web\HiringServlet" method="POST">
            <label for="department"><strong>Department</strong></label><br />
            <select name="department">
                <%String department = request.getParameter("department"); if (department == null) {department = "";}%>     
                <option value="unknown" <%if(department.equals("unknown")){out.print(" selected");}%>>Select a department...</s:option>
                <option value="Human Resources" <%if(department.equals("Human Resources")){out.print(" selected");}%>>Human Resources</s:option>
                <option value="Software Development" <%if(department.equals("Software Development")){out.print(" selected");}%>>Software Development</s:option>
                <option value="Media Relations" <%if(department.equals("Media Relations")){out.print(" selected");}%>>Media Relations</s:option>
            </select><br /><br />

            <%String name = request.getParameter("name");if (name==null) name="";%>
            <label for="name"><strong>Employee Name</strong></label><br />
            <input type="text" name="name" value="<%=name%>" /><br /><br />

            <%String jobTitle = request.getParameter("jobTitle");if (jobTitle == null) jobTitle = "";%>
            <label for="jobTitle"><strong>Job Title</strong></label><br />
            <input type="text" name="jobTitle" value="<%=jobTitle%>" /><br /><br />

            <%String yearHired = request.getParameter("yearHired");if (yearHired == null) yearHired = "";%>
            <label for="yearHired"><strong>Year Hired</strong></label><br />
            <input type="text" name="yearHired" value="<%=yearHired%>" /><br /><br />

            <%String gender = request.getParameter("gender");if (gender == null) gender = "";%>
            <label for="gender"><strong>Gender</strong></label><br />
            <input type="text" name="gender" value="<%=gender%>" /><br /><br />

            <input type="submit" value="Add Employee" /> <input type="reset" value="Reset" />
        </form>

    </body>
</html>

confirmation.jsp: Confirmation.jsp:

<jsp:useBean id="department" scope="request" class="domain.Department"/>
<jsp:useBean id="employee" scope="request" class="domain.Employee"/>
<jsp:useBean id="hiringList" scope="request" class="java.util.ArrayList"/>

<%@page contentType="text/html" pageEncoding="UTF-8" session="false" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Project 1</title>
    </head>
    <body>
        <h1>New Hire Successfully Added</h1>
        <p>
            <strong>Employee Information</strong><br /><br />
            Name: <jsp:getProperty name="employee" property="name"/><br />
            Job Title: <jsp:getProperty name="employee" property="jobTitle"/><br />
            Year Hired: <jsp:getProperty name="employee" property="yearHired"/><br />
            Gender: <jsp:getProperty name="employee" property="gender"/><br /><br />

            Added to department: <jsp:getProperty name="department" property="name"/><br />
        </p>

        <form action="form.jsp" method="POST" style="margin-bottom:25px;">
            <input type="submit" value="Add Another Employee" name="more" />
        </form>

        <hr />

        <h2>Complete Employee List</h2>
        <table>
            <tr><td>Name</td><td>Job Title</td><td>Year Hired</td><td>Gender</td>    <td>Department</td><td>Department Description</td></tr>
            <c:forEach var="hire" items="${hiringList}">
                <tr>
                    <td style="border:1px solid #ccc;padding:0 15px 0 5px;"><c:out value="${hire.getEmployee().getName()}"></c:out></td>
                    <td style="border:1px solid #ccc;padding:0 15px 0 5px;"><c:out value="${hire.getEmployee().getJobTitle()}"></c:out></td>
                    <td style="border:1px solid #ccc;padding:0 15px 0 5px;"><c:out value="${hire.getEmployee().getYearHired()}"></c:out></td>
                    <td style="border:1px solid #ccc;padding:0 15px 0 5px;"><c:out value="${hire.getEmployee().getGender()}"></c:out></td>
                    <td style="border:1px solid #ccc;padding:0 15px 0 5px;"><c:out value="${hire.getDepartment().getName()}"></c:out></td>
                    <td style="border:1px solid #ccc;padding:0 15px 0 5px;"><c:out value="${hire.getDepartment().getDescription()}"></c:out></td>
                </tr>
            </c:forEach>
        </table>
    </body>
</html>

HiringServlet.java: HiringServlet.java:

(imports removed)

public class HiringServlet extends HttpServlet {

public Statement statement;

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

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

        //Declare the dispatcher for the View
        RequestDispatcher view = null; 

        //Create the business logic object
        HiringService hs = null;

        //Create the Status object and store it in the request 
        //for use by the 'Registration Form' View (if necessary)
        Status status = new Status();
        request.setAttribute("status", status);

        //Retrieve the HTML form parameters
        String departmentName = request.getParameter("department");
        String name = request.getParameter("name");
        String jobTitle = request.getParameter("jobTitle");
        String yearHired = request.getParameter("yearHired");
        String gender = request.getParameter("gender");

        // Verify form fields data; create Exception objects if data are missing
        if (departmentName.equals("unknown")) 
            status.addException(new Exception("Please select a department. "));
        if ((name == null) || (name.length() == 0))
            status.addException(new Exception("Please enter the employee name. "));
        if ((jobTitle == null) || (jobTitle.length() == 0))
            status.addException(new Exception("Please enter the job title. "));
        if ((yearHired == null) || (yearHired.length() == 0))
            status.addException(new Exception("Please enter the year hired. "));
        if ((gender == null) || (gender.length() == 0))
            status.addException(new Exception("Please enter the gender. "));

        // In case of errors, forward the request back to 'Registration Form' 
        // View and return without proceeding with the rest of the business logic.
        if(!status.isSuccessful()){
            view = request.getRequestDispatcher("form.jsp");              
            view.forward(request, response);
            return;
        }

        //If no verification errors are found, the Controller
        //uses the business services to perform the registration.
        try {

      // Load the JDBC driver
      Class.forName("oracle.jdbc.driver.OracleDriver");
      System.out.println("Driver loaded");

      // Establish a connection
      Connection connection = DriverManager.getConnection ("jdbc:oracle:thin:@nova.umuc.edu:1521:acad", "username", "password");
      System.out.println("Database connected");

      // Create a statement
      statement = connection.createStatement();

            hs = new HiringService();

            // Retrieve the department object and verify that it exists.
            Department department = hs.getDepartment(departmentName);

            if (department == null) throw new Exception("The department you have "+
               " selected does not yet exist; please select another one.");

            //Create and populate the employee object
            Employee employee = hs.createEmployee(
                name, jobTitle, yearHired, gender);

            //Delegate hiring to the HiringService object.
            Hiring newHire = new Hiring(employee, department);
            hs.hire(newHire);

            ArrayList hiringList = hs.getHirings();

            request.setAttribute( "department", department);
            request.setAttribute( "employee", employee);
            request.setAttribute( "hiringList", hiringList);

            // Select the next View for the user in case hiring is 
            // successful Forward to the confirmation.jsp View       
            view = request.getRequestDispatcher("/confirmation.jsp");         
            view.forward(request,response);
        } 

        catch (Exception ex){
            status.addException(ex);

            //Select next View.
            //Exceptions are caught, forward back to the form.jsp View.  
            view = request.getRequestDispatcher("/form.jsp");           
            view.forward(request,response);
        }

        finally {            
            out.close();
        }
    }
}

If you need to see any of my other .java documents, let me know and I'll post them, but I don't think they're the problem. 如果您需要查看其他任何.java文档,请告诉我,我将其发布,但我认为这不是问题。

I would start from here 我将从这里开始

<form action="C:\Users\Nelle\Documents\NetBeansProjects\EmployeeDB\src\java\web\HiringServlet" method="POST">

Do you have any clue about your HiringServlet being called? 您是否有关于HiringServlet被调用的线索?

Configure the Servlet in web.xml and give the url pattern in "form" tag in "action" attribute. 在web.xml中配置Servlet,并在“ action”属性的“ form”标签中提供url模式。

Look at the link. 查看链接。

How to configurate a servlet path in web.xml 如何在web.xml中配置servlet路径

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM