无法编译第一个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代码都在<% %>内,但我无法绕过这个。在服务器上运行程序后,错误是HTTP状态500。无法为JSP编译类

您在else行上缺少一个大括号。改成:

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

永远不要使用script,而是使用更容易使用和更少出错的JavaServer Pages标准标签库或表达式语言。

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

更改:(正确版本)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>

<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 -隐式对象

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

最新更新