一直报错不知道问题在哪里

来源:3-3 综合训练-实现新增员工功能

qq_ibertine_0

2020-09-11 10:25:34

package com.imooc.employee;


import java.io.IOException;

import java.util.ArrayList;

import java.util.List;


import javax.servlet.ServletContext;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


/**

 * Servlet implementation class EPservlet

 */

@WebServlet("/anno")

public class EPservlet extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public EPservlet() {

        super();

        // TODO Auto-generated constructor stub

    }


/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*/

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

ServletContext context=request.getServletContext();

if(context.getAttribute("employees")==null) {

List list=new ArrayList();

list.add(new Employee(001,"小李","技术部","工程师",521521f));

list.add(new Employee(002,"小万","运营部","工程师",999999f));

context.setAttribute("employees", list);

}

request.getRequestDispatcher("/employee.jsp").forward(request, response);

}


}

package com.imooc.employee;


public class Employee {

     private Integer no;

     private String name;

     private String department;

     private String job;

     private Float salary;

public Employee(Integer no, String name, String department, String job, Float salary) {

super();

this.no = no;

this.name = name;

this.department = department;

this.job = job;

this.salary = salary;

}

public Integer getNo() {

return no;

}

public void setNo(Integer no) {

this.no = no;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getDepartment() {

return department;

}

public void setDepartment(String department) {

this.department = department;

}

public String getJob() {

return job;

}

public void setJob(String job) {

this.job = job;

}

public Float getSalary() {

return salary;

}

public void setSalary(Float salary) {

this.salary = salary;

}

}

<%@ page contentType="text/html;charset=UTF-8"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width,initial-scale=1">

    <title>员工列表</title>

    <link href="css/bootstrap.css" type="text/css" rel="stylesheet"></link>

    

    <script type="text/javascript" src="js/jquery-1.11.1.min.js"></script>

    <script type="text/javascript" src="js/bootstrap.js"></script>


    <style type="text/css">

        .pagination {

            margin: 0px

        }


        .pagination > li > a, .pagination > li > span {

            margin: 0 5px;

            border: 1px solid #dddddd;

        }


        .glyphicon {

            margin-right: 3px;

        }


        .form-control[readonly] {

            cursor: pointer;

            background-color: white;

        }

        #dlgPhoto .modal-body{

            text-align: center;

        }

        .preview{


            max-width: 500px;

        }

    </style>

    <script>

        $(function () {

            

            $("#btnAdd").click(function () {

                $('#dlgForm').modal()

            });

        })



    </script>

</head>

<body>


<div class="container">

    <div class="row">

        <h1 style="text-align: center">IMOOC员工信息表</h1>

        <div class="panel panel-default">

            <div class="clearfix panel-heading ">

                <div class="input-group" style="width: 500px;">

                    <button class="btn btn-primary" id="btnAdd"><span class="glyphicon glyphicon-zoom-in"></span>新增

                    </button>

                </div>

            </div>


            <table class="table table-bordered table-hover">

                <thead>

                <tr>

                    <th>序号</th>

                    <th>员工编号</th>

                    <th>姓名</th>

                    <th>部门</th>

                    <th>职务</th>

                    <th>工资</th>

                    <th>&nbsp;</th>

                </tr>

                </thead>

                <tbody>

                <c:forEach items="${applicationScope.employees }" var="emp" varStatus="idx">

                <tr>

                    <td>${idx.index+1 }</td>

                    <td>${emp.no }</td>

                    <td>${emp.name }</td>

                    <td>${emp.department }</td>

                    <td>${emp.job }</td>

                    <td style="color: red;font-weight: bold">¥<fmt:formatNumber value="${emp.salary }" pattern="0,000.00"></fmt:formatNumber></td>

                    

                </tr>

                </c:forEach>

               

                </tbody>

            </table>

        </div>

    </div>

</div>


<!-- 表单 -->

<div class="modal fade" tabindex="-1" role="dialog" id="dlgForm">

    <div class="modal-dialog" role="document">

        <div class="modal-content">

            <div class="modal-header">

                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>

                </button>

                <h4 class="modal-title">新增员工</h4>

            </div>

            <div class="modal-body">

                <form action="#" method="post" >

                    <div class="form-group">

                        <label for="empno">员工编号</label>

                        <input type="text" name="empno" class="form-control" id="empno" placeholder="请输入员工编号">

                    </div>

                    <div class="form-group">

                        <label for="ename">员工姓名</label>

                        <input type="text" name="ename" class="form-control" id="ename" placeholder="请输入员工姓名">

                    </div>

                    <div class="form-group">

                        <label>部门</label>

                        <select id="dname" name="department" class="form-control">

                            <option selected="selected">请选择部门</option>

                            <option value="市场部">市场部</option>

                            <option value="研发部">研发部</option>

                        <option value="后勤部">后勤部</option>

                        </select>

                    </div>


                    <div class="form-group">

                        <label>职务</label>

                        <input type="text" name="job" class="form-control" id="sal" placeholder="请输入职务">

                    </div>


                    <div class="form-group">

                        <label for="sal">工资</label>

                        <input type="text" name="salary" class="form-control" id="sal" placeholder="请输入工资">

                    </div>


                    <div class="form-group" style="text-align: center;">

                        <button type="submit" class="btn btn-primary">保存</button>

                    </div>

                </form>

            </div>


        </div><!-- /.modal-content -->

    </div><!-- /.modal-dialog -->

</div><!-- /.modal -->



</body>

</html>

package com.imooc.employee;


import java.io.IOException;

import java.util.List;


import javax.servlet.ServletContext;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


/**

 * Servlet implementation class CreatServlet

 */

@WebServlet("/CS")

public class CreatServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public CreatServlet() {

        super();

        // TODO Auto-generated constructor stub

    }


/**

* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

*/

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

// TODO Auto-generated method stub

request.setCharacterEncoding("UTF-8");

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

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

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

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

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

ServletContext context=request.getServletContext();

Employee employee=new Employee(Integer.parseInt(empno),ename,department,job,Float.parseFloat(salary));

List list1= (List)context.getAttribute("employees");

list1.add(employee);

request.setAttribute("emp", list1);

request.getRequestDispatcher("/employee.jsp").forward(request, response);

}


}

http://img.mukewang.com/climg/5f5ae01209cdd84317010859.jpghttp://img.mukewang.com/climg/5f5ae01c09f5b35311550391.jpg

写回答

2回答

好帮手慕阿园

2020-09-11

同学你好,老师复制同学的代码是可以访问的,如下

http://img.mukewang.com/climg/5f5b180f09427e3018410672.jpg

同学可以从以下几个方面排查

1,确认修改完代码后进行了保存

2,清除下eclispe和浏览器的缓存

http://img.mukewang.com/climg/5f5b186609cefbf602710277.jpg

http://img.mukewang.com/climg/5f5b1866096173cf05190305.jpg

3,如果还不可以,重新创建一个项目试试

4,最后导入源码,看看可不可以正常访问

祝学习愉快

0
hq_ibertine_0
h 清楚了缓存然后好使了我添加完数据点击保存后出现了405 HTTP状态 405 - 方法不允许 类型 状态报告 消息 此URL不支持Http方法POST 描述 请求行中接收的方法由源服务器知道,但目标资源不支持
h020-09-11
共1条回复

好帮手慕阿园

2020-09-11

同学你好,这里可以新建一个doPost方法,在doPost方法中调用doGet方法,如:

http://img.mukewang.com/climg/5f5b5262094e16e909990200.jpg

并且页面上form表单的action应该跳转到对应的页面上,如

http://img.mukewang.com/climg/5f5b530a095814cc05640117.jpg

祝学习愉快

0

0 学习 · 9666 问题

查看课程