无法编译我的第一个JSP程序

时间:2014-08-16 11:37:56

标签: java jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <!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>Insert title here</title>
</head>
 <body>


  <form method=post action="Check.jsp">


 <center><h3>Voter Application</h3></center>
 Enter your Age:<input type="text" name="age">
 <input type="submit" value = "Check Age">
 </form> 


 </body>

第二个jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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>Insert title here</title>
</head>


<body>
<% int age = Integer.parseInt(request.getParametes(age));
    if(age>=18){
%><h1>You are eligible to vote</h1>
<% else{ %> <h2>Sorry, you cant vote yet</h2>
<%} %>
</body>
</html>



     </html>

以下是错误:第二个JSP在else的结束大括号中显示编译错误。所有的java代码都在&lt; %%&gt;范围内按照规则但我无法解决这个问题。在服务器上运行程序后,错误是HTTP状态500。无法编译JSP的类

2 个答案:

答案 0 :(得分:0)

您在else行上缺少大括号。改为:

<% } else { %> <h2>Sorry, you cant vote yet</h2>

答案 1 :(得分:0)

永远不要使用Scriplet,而是使用更易于使用且不易出错的JavaServer Pages Standard Tag LibraryExpression Language

您可以使用<c:if><c:choose>

更改:(正确版本)1。request.getParameter("age") 2. } else {

<% int age = Integer.parseInt(request.getParameter("age"));
if(age>=18){%>
       <h1>You are eligible to vote</h1>
<%} else { %>
       <h2>Sorry, you cant vote yet</h2>
<%} %>

要:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:choose>
    <c:when test="${param.age>=18 }">
        <h1>You are eligible to vote</h1>
    </c:when>
    <c:otherwise>
        <h1>You are eligible to vote</h1>
    </c:otherwise>
</c:choose>

OR

<c:if test="${param.age>=18 }">
    <h1>You are eligible to vote</h1>
</c:if>
<c:if test="${param.age<18 }">
    <h1>Sorry, you cant vote yet</h1>
</c:if>

详细了解JSP - Implicit Objects

  

param:将请求参数名称映射到单个值