//VoterServlet.java
package com.nt.servlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class VoterServlet extends HttpServlet
{
public void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException{
PrintWriter pw=null;
String name=null,tage=null;
int age=0;
//get PrintWriter
pw=res.getWriter();
//set response content type
res.setContentType("text/html");
//get request parameter values(form data)
name=req.getParameter("name");
tage=req.getParameter("age");
age=Integer.parseInt(age);
//write request processing logic
if(age>=18){
pw.println("<h1 style='color:green'>"+name+"You are ELIGIBLE to vote</h1>");
}
else{
pw.println("<h1 style='color:red'>"+name+"You are not ELIGIBLE to vote</h1>");
}
//add hyperlink
pw.println("<a href='input.html'><img src='guns-49a.jpg'></a>");
//close stream
pw.close();
}//doPost(-,-)
}//class
错误:不兼容的类型:int无法转换为字符串 age = Integer.parseInt(age);
答案 0 :(得分:0)
您的代码中存在错误
请更正
tage=req.getParameter("age");
age=Integer.parseInt(tage);//use 'tage' instead 'age'
在这里,你希望从“年龄”中获得年龄。变量为&#39; age&#39;变量作为字符串的整数值。