Java 从模型到Servlet再到JSP选项列表获取信息
我有一个模型,里面有一个arraylist。我还有一个servlet需要从模型中提取。最后,JSP需要获取从servlet(控制器)检索到的信息,并为选择列表生成选项。我不明白为什么这根本不起作用。我刚刚进入jsp的,所以任何帮助将不胜感激 我有下面的3页和stacktrace 所建议的任何有助于未来发展的变化也会有所帮助。我非常开放,希望用正确的方式来做这件事 模型(具有arraylist)Java 从模型到Servlet再到JSP选项列表获取信息,java,jsp,jakarta-ee,servlets,arraylist,Java,Jsp,Jakarta Ee,Servlets,Arraylist,我有一个模型,里面有一个arraylist。我还有一个servlet需要从模型中提取。最后,JSP需要获取从servlet(控制器)检索到的信息,并为选择列表生成选项。我不明白为什么这根本不起作用。我刚刚进入jsp的,所以任何帮助将不胜感激 我有下面的3页和stacktrace 所建议的任何有助于未来发展的变化也会有所帮助。我非常开放,希望用正确的方式来做这件事 模型(具有arraylist) 包edu.witc.Assignment03.model; 导入java.util.ArrayList
包edu.witc.Assignment03.model;
导入java.util.ArrayList;
导入java.util.List;
公共阶级国家{
私有列表状态=新的ArrayList(){
添加(“阿拉巴马州”);
添加(“阿拉斯加”);
添加(“亚利桑那州”);
添加(“阿肯色州”);
添加(“加利福尼亚”);
添加(“科罗拉多”);
添加(“康涅狄格州”);
添加(“特拉华州”);
添加(“佛罗里达”);
添加(“格鲁吉亚”);
添加(“夏威夷”);
添加(“爱达荷州”);
添加(“伊利诺伊州”);
添加(“印第安纳州”);
添加(“爱荷华州”);
添加(“堪萨斯”);
添加(“肯塔基州”);
添加(“路易斯安那州”);
添加(“缅因州”);
添加(“马里兰州”);
添加(“马萨诸塞州”);
添加(“密歇根”);
添加(“明尼苏达州”);
添加(“密西西比”);
添加(“密苏里州”);
添加(“蒙大拿”);
添加(“内布拉斯加州”);
添加(“内华达州”);
添加(“新罕布什尔州”);
添加(“新泽西”);
添加(“新墨西哥州”);
添加(“纽约”);
添加(“北卡罗来纳州”);
添加(“北达科他州”);
添加(“俄亥俄州”);
添加(“俄克拉荷马州”);
添加(“俄勒冈州”);
添加(“宾夕法尼亚州”);
添加(“罗德岛”);
添加(“南卡罗来纳州”);
添加(“南达科他州”);
添加(“田纳西州”);
添加(“德克萨斯州”);
添加(“犹他州”);
添加(“佛蒙特州”);
添加(“弗吉尼亚”);
添加(“华盛顿”);
添加(“西弗吉尼亚”);
添加(“威斯康星州”);
添加(“怀俄明州”);
}
公共列表getStates(){
把这封信寄回美国;
}
}
控制器(Servlet)
包edu.witc.Assignment03.controller;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.List;
导入javax.servlet.*;
导入javax.servlet.annotation.WebServlet;
导入javax.servlet.http.HttpServlet;
导入javax.servlet.http.HttpServletRequest;
导入javax.servlet.http.HttpServletResponse;
//导入javax.servlet.annotation.WebServlet;
//导入javax.servlet.http.HttpServlet;
//导入javax.servlet.http.HttpServletRequest;
//导入javax.servlet.http.HttpServletResponse;
导入edu.witc.Assignment03.model.Customer;
导入edu.witc.Assignment03.model.Phone;
导入edu.witc.Assignment03.model.States;
/*
*不是线程安全的。仅供说明之用
*/
@WebServlet(name=“CustomerServlet”,urlPatterns={
“/customerManagement”})
公共类CustomerServlet扩展了HttpServlet{
私有静态最终长serialVersionUID=-20L;
private edu.witc.Assignment03.model.States=新州();
private List customers=new ArrayList();
私有void addCustomer(HttpServletResponse,HttpServletRequest)//重定向到索引
抛出IOException、ServletException{
字符串url=“/customerManagement.jsp”;
request.setAttribute(“客户”,客户);
getRequestDispatcher(url).forward(请求,响应);
}
私有void editCustomer(HttpServletResponse,HttpServletRequest)//重定向到索引
抛出IOException、ServletException{
字符串url=“/customerManagement.jsp”;
request.setAttribute(“客户”,客户);
getRequestDispatcher(url).forward(请求,响应);
}
私有void sendCustomerList(HttpServletResponse,HttpServletRequest)//重定向到索引
抛出IOException、ServletException{
字符串url=“/index.jsp”;
request.setAttribute(“客户”,客户);
getRequestDispatcher(url).forward(请求,响应);
}
私人客户getCustomer(int customerId){
用于(客户:客户){
if(customer.getCustomerId()==customerId){
退货客户;
}
}
返回null;
}
私有void sendEditCustomerForm(HttpServletRequest请求,
HttpServletResponse响应)引发IOException,ServletException{
字符串url=“/customerManagement.jsp”;
request.setAttribute(“客户”,客户);
getRequestDispatcher(url).forward(请求,响应);
}
公共无效数据集(HttpServletRequest请求,
HttpServletResponse(响应)
抛出ServletException、IOException{
字符串uri=request.getRequestURI();
if(uri.endsWith(“/customer”)){
sendCustomerList(响应、请求);
}else if(uri.endsWith(“/editCustomer”)){
SendEditCustomPerform(请求、响应);
}
setAttribute(“states”,states);
}
public void doPost(HttpServletRequest请求,
HttpServletResponse(响应)
抛出ServletException、IOException{
//更新客户
int customerId=0;
试一试{
客户ID=
Integer.parseInt(request.getParameter(“id”);
}捕获(数字格式){
}
Customer=getCustomer(customerId);
如果(客户!=null){
customer.setFirstName(request.getParameter(“firstName”);
customer.setLastName(request.getParameter(“lastName”);
customer.setEmail(request.getPar
package edu.witc.Assignment03.model;
import java.util.ArrayList;
import java.util.List;
public class States {
private List<String> states = new ArrayList<>();{
states.add("Alabama");
states.add("Alaska");
states.add("Arizona");
states.add("Arkansas");
states.add("California");
states.add("Colorado");
states.add("Connecticut");
states.add("Delaware");
states.add("Florida");
states.add("Georgia");
states.add("Hawaii");
states.add("Idaho");
states.add("Illinois");
states.add("Indiana");
states.add("Iowa");
states.add("Kansas");
states.add("Kentucky");
states.add("Louisiana");
states.add("Maine");
states.add("Maryland");
states.add("Massachusetts");
states.add("Michigan");
states.add("Minnesota");
states.add("Mississippi");
states.add("Missouri");
states.add("Montana");
states.add("Nebraska");
states.add("Nevada");
states.add("New Hampshire");
states.add("New Jersey");
states.add("New Mexico");
states.add("New York");
states.add("North Carolina");
states.add("North Dakota");
states.add("Ohio");
states.add("Oklahoma");
states.add("Oregon");
states.add("Pennsylvania");
states.add("Rhode Island");
states.add("South Carolina");
states.add("South Dakota");
states.add("Tennessee");
states.add("Texas");
states.add("Utah");
states.add("Vermont");
states.add("Virginia");
states.add("Washington");
states.add("West Virginia");
states.add("Wisconsin");
states.add("Wyoming");
}
public List<String> getStates(){
return this.states;
}
}
package edu.witc.Assignment03.controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//import javax.servlet.annotation.WebServlet;
//import javax.servlet.http.HttpServlet;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
import edu.witc.Assignment03.model.Customer;
import edu.witc.Assignment03.model.Phone;
import edu.witc.Assignment03.model.States;
/*
* Not thread-safe. For illustration purpose only
*/
@WebServlet(name = "CustomerServlet", urlPatterns = {
"/customerManagement"})
public class CustomerServlet extends HttpServlet {
private static final long serialVersionUID = -20L;
private edu.witc.Assignment03.model.States states = new States();
private List<edu.witc.Assignment03.model.Customer> customers = new ArrayList<Customer>();
private void addCustomer(HttpServletResponse response, HttpServletRequest request)//redirect to index
throws IOException, ServletException {
String url = "/customerManagement.jsp";
request.setAttribute("customers", customers);
request.getRequestDispatcher(url).forward(request,response);
}
private void editCustomer(HttpServletResponse response, HttpServletRequest request)//redirect to index
throws IOException, ServletException {
String url = "/customerManagement.jsp";
request.setAttribute("customers", customers);
request.getRequestDispatcher(url).forward(request,response);
}
private void sendCustomerList(HttpServletResponse response, HttpServletRequest request)//redirect to index
throws IOException, ServletException {
String url = "/index.jsp";
request.setAttribute("customers", customers);
request.getRequestDispatcher(url).forward(request,response);
}
private Customer getCustomer(int customerId) {
for (Customer customer : customers) {
if (customer.getCustomerId() == customerId) {
return customer;
}
}
return null;
}
private void sendEditCustomerForm(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
String url = "/customerManagement.jsp";
request.setAttribute("customers", customers);
request.getRequestDispatcher(url).forward(request,response);
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String uri = request.getRequestURI();
if (uri.endsWith("/customer")) {
sendCustomerList(response, request);
} else if (uri.endsWith("/editCustomer")) {
sendEditCustomerForm(request, response);
}
request.setAttribute("states", states);
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// update customer
int customerId = 0;
try {
customerId =
Integer.parseInt(request.getParameter("id"));
} catch (NumberFormatException e) {
}
Customer customer = getCustomer(customerId);
if (customer != null) {
customer.setFirstName(request.getParameter("firstName"));
customer.setLastName(request.getParameter("lastName"));
customer.setEmail(request.getParameter("email"));
customer.setPhone(request.getParameter("phone"));
customer.setAddress(request.getParameter("address"));
customer.setCity(request.getParameter("city"));
customer.setZip(request.getParameter("zip"));
}
addCustomer(response, request);
}
}
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.ArrayList" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Customer Management</title>
</head>
<body>
<form action="/CustomerServlet" method="post">
First Name:<br>
<input type="text" name="firstName"/><br>
Last Name:<br>
<input type="text" name="lastName"/><br>
Email:<br>
<input type="text" name="email"/><br>
Phone:<br>
<input type="text" name="phone"/><br>
Phone Type:<br>
Street Address:<br>
<input type="text" name="streetAddress"/><br>
Apartment Number:<br>
<input type="text" name="apartmentNumber"/><br>
City:<br>
<input type="text" name="city"/><br>
State:<br>
<select>
<%
edu.witc.Assignment03.model.States states = request.getAttribute("states");
if(states!=null){
for (String state : states.getStates()) {
out.println("<option>"+state+"</option>");
}
}else{
System.out.print("states is null");
}
%>
</select><br>
<input type="submit" value="submit">
</form>
</body>
</html>
Mar 30, 2014 8:17:13 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [jsp] in context with path [/Justin_EJ_Assignment03_15400579] threw exception [Unable to compile class for JSP:
An error occurred at line: 33 in the jsp file: /customerManagement.jsp
Type mismatch: cannot convert from Object to States
30: State:<br>
31: <select>
32: <%
33: edu.witc.Assignment03.model.States states = request.getAttribute("states");
34: if(states!=null){
35: for (String state : states.getStates()) {
36: out.println("<option>"+state+"</option>");
Stacktrace:] with root cause
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 33 in the jsp file: /customerManagement.jsp
Type mismatch: cannot convert from Object to States
30: State:<br>
31: <select>
32: <%
33: edu.witc.Assignment03.model.States states = request.getAttribute("states");
34: if(states!=null){
35: for (String state : states.getStates()) {
36: out.println("<option>"+state+"</option>");
Stacktrace:
at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
at org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:366)
at org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:468)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:378)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:657)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
<c:forEach var="state" items="states">
<option>
<c:out value="${person.name}" />
</option>
</c:forEach>