我的最后一个java问题,即使是我的教授也无法解决';解决不了
我正在写我的最后一个java作业(Yayy!-感谢所有通过其他作业帮助我的人)。在本例中,我必须编写一个servlet,它获取年龄、婚姻状况、房屋收入和孩子数量,并输出到数据库,然后将更新后的平均值返回给用户。然而,我遇到了这样的情况:我的最后一个java问题,即使是我的教授也无法解决';解决不了,java,servlets,Java,Servlets,我正在写我的最后一个java作业(Yayy!-感谢所有通过其他作业帮助我的人)。在本例中,我必须编写一个servlet,它获取年龄、婚姻状况、房屋收入和孩子数量,并输出到数据库,然后将更新后的平均值返回给用户。然而,我遇到了这样的情况: java.lang.NoClassDefFoundError: HouseSurvey$SurveyResults at HouseSurvey.doPost(HouseSurvey.java:23) at javax.servlet.http.
java.lang.NoClassDefFoundError: HouseSurvey$SurveyResults
at HouseSurvey.doPost(HouseSurvey.java:23)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.servlets.InvokerServlet.serveRequest(InvokerServlet.java:419)
at org.apache.catalina.servlets.InvokerServlet.doPost(InvokerServlet.java:169)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:868)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:663)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: HouseSurvey$SurveyResults
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1338)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1187)
... 21 more
在过去,我曾因发布太多代码、代码不够等而感到内疚。我为自己的幼稚道歉。以下是我写的课程:
import java.text.DecimalFormat;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HouseSurvey extends HttpServlet {
private final static String SURVEY_FILE = "HouseSurvey.dat";
SurveyResults results;
Household h;
DecimalFormat form = new DecimalFormat("#,###,#00.00");
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter(); //make a printwriter to print the page
File survey = new File(SURVEY_FILE);
if (!survey.exists()) //check to see if the file exists
results = new SurveyResults();
else { //If the file exists, read it the latest survey tallies
try {
ObjectInputStream ips = new ObjectInputStream(new FileInputStream(survey));
results = (SurveyResults) ips.readObject(); //read the file into 'results'
ips.close(); //close the input stream
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (FileNotFoundException f) {
f.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
int ageValue = Integer.parseInt(request.getParameter("age")),
childValue = Integer.parseInt(request.getParameter("children"));
double incomeValue = Double.parseDouble(request.getParameter("income"));
Boolean marriedValue;
if (request.getParameter("status") == "married") {
marriedValue = true;
} else {
marriedValue = false;
}
Household h = new Household(ageValue, childValue, incomeValue, marriedValue);
results.totalResults(h);
//Write results to disk.
try {
ObjectOutputStream ops = new ObjectOutputStream(new FileOutputStream(survey));
ops.writeObject(results);
ops.flush();
ops.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
response.setContentType("text/html"); //contnent type for the responding webpage
out.println("<html>\n" +
"<head><title>Thanks!</tile></head>" +
"<body>" +
" <h1 style='text-align:center'>RRC BIT Department - Household Survey</h1>" +
" <hr><br/>" +
" <h2 style='text-align:center'>Up to Date Survey Results</h2>" +
" <h4 style='left-margin:200px'>Average Age: " + results.getAvgAge() +
" <h4 style='left-margin:200px'>Average Number of Children: " + results.getAvgKids() +
" <h4 style='left-margin:200px'>Average Number of Married Respondents: " + results.getAvgKids() +
" <h4 style='left-margin:200px'>Average Number of Single Respondents: " + results.getTotalSingle() +
" <h4 style='left-margin:200px'>Average Income: " + results.getAvgIncome());
}
private class Household {
private int age, children;
private double income;
private boolean married = false;
/**
* Method: Household
* Constructor
*
* @ param age - age of person surveyed:int
* @ param children - number of children person surveyed has:int
* @ param married - true or false, used to determine married or single:boolean
* @ param income - the family income of the person surveyed:double
*/
private Household(int age, int children, double income, boolean married) {
this.age = age;
this.children = children;
this.income = income;
this.married = married;
}
//Getters
private int getAge() {
return age;
}
private int getChildren() {
return children;
}
private double getIncome() {
return income;
}
private boolean getMarried() {
return married;
}
}
private class SurveyResults implements Serializable {
private double totalAge, totalChildren, totalMarried, totalSingle, totalIncome;
/**
* Method: SurveyResults
* Used for totals
*
* @ param h - Household object created above:Household
*/
private void totalResults(Household h) {
if (h.getMarried()) totalMarried += 1;
else totalSingle += 1;
totalAge += h.getAge();
totalChildren += h.getChildren();
totalIncome += h.getIncome();
}
private double getTotalHouseholds() {
return totalSingle + totalMarried;
}
private double getAvgAge() {
return totalAge / getTotalHouseholds();
}
private double getAvgKids() {
return totalChildren / getTotalHouseholds();
}
private double getTotalMarried() {
return totalMarried;
}
private double getTotalSingle() {
return totalSingle;
}
private double getAvgIncome() {
return totalIncome / getTotalHouseholds();
}
}
}
导入java.text.DecimalFormat;
导入java.io.*;
导入javax.servlet.*;
导入javax.servlet.http.*;
公共类HouseSurvey扩展了HttpServlet{
私有最终静态字符串测量\u FILE=“HouseSurvey.dat”;
调查结果;
住户h;
DecimalFormat form=新的DecimalFormat(“#,#,#,#,#00.00”);
public void doPost(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException{
PrintWriter out=response.getWriter();//制作一个PrintWriter来打印页面
文件测量=新文件(测量文件);
if(!survey.exists())//检查文件是否存在
结果=新的调查结果();
否则{//如果文件存在,请读取最新的调查结果
试一试{
ObjectInputStream ips=新ObjectInputStream(新文件InputStream(调查));
results=(SurveyResults)ips.readObject();//将文件读入“results”
ips.close();//关闭输入流
}catch(classnotfounde异常){
e、 printStackTrace();
}捕获(FileNotFoundException f){
f、 printStackTrace();
}捕获(ioe异常ioe){
ioe.printStackTrace();
}
}
int ageValue=Integer.parseInt(request.getParameter(“age”),
childValue=Integer.parseInt(request.getParameter(“children”);
double incomeValue=double.parseDouble(request.getParameter(“收入”));
布尔值;
if(request.getParameter(“status”)=“已婚”){
婚姻价值=真;
}否则{
婚姻价值=假;
}
住户h=新住户(年龄价值、子女价值、收入价值、婚姻价值);
结果:总结果(h);
//将结果写入磁盘。
试一试{
ObjectOutputStream ops=新的ObjectOutputStream(新文件OutputStream(调查));
操作写入对象(结果);
ops.flush();
ops.close();
}捕获(ioe异常ioe){
ioe.printStackTrace();
}
response.setContentType(“text/html”);//响应网页的内容类型
out.println(“\n”+
“谢谢!”+
"" +
“RRC BIT部门-住户调查”+
“
”+
“最新调查结果”+
“平均年龄:”+结果。getAvgAge()+
“平均子项数:”+results.getAvgKids()+
“已婚受访者的平均数量:”+results.getAvgKids()+
“单一受访者的平均人数:”+results.getTotalSingle()+
“平均收入:”+results.getAvgIncome());
}
私人阶级家庭{
私人年龄、儿童;
私人双收入;
私有布尔值=假;
/**
*方法:住户
*建造师
*
*@param age-被调查者的年龄:int
*@param children-被调查的儿童人数:int
*@param married-true或false,用于确定已婚或单身:布尔值
*@param income-被调查者的家庭收入:双倍
*/
私人家庭(整数年龄、整数子女、双倍收入、已婚){
这个。年龄=年龄;
这个。孩子=孩子;
这个。收入=收入;
这是已婚的;
}
//吸气剂
私有int getAge(){
回归年龄;
}
private int getChildren(){
返回儿童;
}
私人双收入{
收益;
}
私有布尔getMarried(){
结婚归来;
}
}
私有类SurveyResults实现可序列化{
私人双倍总年龄、总子女、总已婚、总单身、总收入;
/**
*方法:调查结果
*用于合计
*
*@param h-在上面创建的家庭对象:家庭
*/
私人收入(家庭h){
如果(h.getMarried())totalMarried+=1;
else TOTALL SINGLE+=1;
totalAge+=h.getAge();
totalChildren+=h.getChildren();
总收入+=h.getIncome();
}
私有双getTotalHouseholds(){
返回totalSingle+Total已婚;
}
私有双getAvgAge(){
返回totalAge/getTotalHouseholds();
}
私有双getAvgKids(){
返回totalChildren/getTotalHouseholds();
}
私有双getTotalMarried(){
回归婚姻;
}
私有双getTotalSingle(){
返回totalSingle;
}
私有双getAvgIncome(){
返回totalIncome/getTotalHouseholds();
}
}
}
我觉得很接近,所以我去问我的教练。他看了大约20分钟,但他想不出来
我知道你们真是太聪明了(特别是当涉及到东西的时候),所以如果你们中的任何人能抽出一点时间来帮助一个兄弟……看起来你们并没有上传所有编译的
.class
文件
java.lang.NoClassDefFoundError: HouseSurvey$SurveyResults