`我是一個新的java和servlet主題。 我寫了一個小的web應用程序。它從Web表單收集一些數據並將其添加到LinkedList中。 但我的調試器顯示我添加到LL不會發生。請幫助!LinkedList && Servlet
這裏是一些代碼:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
String surname = request.getParameter("surname");
String fatherName = request.getParameter("fathername");
String department = request.getParameter("department").toLowerCase();
Integer age = Integer.valueOf(request.getParameter("age"));
System.out.println(name);
System.out.println(surname);
System.out.println(fatherName);
responseToUser(addEmployee(name,surname,fatherName,department, age),response);
}
public boolean addEmployee(String name, String surname, String fathername, String department, Integer age) {
AllWorkers workers = new AllWorkers();
if (!name.equals("") && !surname.equals("") && !fathername.equals("") && !dep artment.equals("") && age != null) {
Person person = new Person(surname, name, fathername, department, age);
Departments dep = Departments.valueOf(person.getDepartment());
if (dep == Departments.логистика) {
workers.addLogistic(person);
} else if (dep == Departments.продажи) {
workers.addSales(person);
}
return true;
} else {
return false;
}
}
public void responseToUser(boolean boo, HttpServletResponse response) throws IOException {
Gson gson = new Gson();
response.setContentType("application/json");
if(boo == true){
response.getWriter().write(gson.toJson(true));
}else if(boo == false){
response.getWriter().write(gson.toJson(false));
}
}
部門:
public enum Departments {
логистика,продажи
}
AllWorkers.java
public class AllWorkers {
LinkedList<Person> logistic = new LinkedList<Person>();
LinkedList<Person> sales = new LinkedList<Person>();
public void addLogistic(Person person){
logistic.add(person);
}
public void addSales(Person person){sales.add(person);}
public LinkedList<Person> getLogistic() {
return logistic;
}
public LinkedList<Person> getSales() {
return sales;
}
}
Person.java
public class Person {
private String name;
private String surname;
private String fatherName;
private String department;
private int age;
Person(){
}
Person(String name, String surname, String fatherName,String department, int age){
this.name = name;
this.surname = surname;
this.fatherName = fatherName;
this.department = department;
this.age = age;
}
public String getDepartment() {
return department;
}
public String getFatherName() {
return fatherName;
}
public int getAge() {
return age;
}
public String getSurname() {
return surname;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setFatherName(String fatherName) {
this.fatherName = fatherName;
}
public void setSurname(String surname) {
this.surname = surname;
}
public void setDepartment(String department) {
this.department = department;
}
}
我們需要更多信息。例如:你的LinkedList在哪裏? 'Departments.valueOf()'方法的實現是什麼? –
你可以使用response.getWriter()。write(gson.toJson(boo));而不是if(boo == true) – StanislavL
你會得到什麼迴應? – Priyesh