<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" isErrorPage="false"%>
<%@page import="java.security.MessageDigest"%><%!
private static final String KEY = ":[email protected]";

public final static String calcMD5(String ss){
String s = ss==null?"":ss;
char hexDigits[] = {'0','1','2','3','4','5','6','7','8','9',
'a','b','c','d','e','f','g'};
try {
byte[] strTmp = s.getBytes();
MessageDigest mdTmp = MessageDigest.getInstance("MD5");
mdTmp.update(strTmp);

byte[] md = mdTmp.digest();
int j = md.length;
char str[] = new char[j*2];
int k = 0;
for(int i = 0; i< j; i++){
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
}catch(Exception e){
return null;
}
}
 %>
 <%
  request.setCharacterEncoding("UTF-8");
  response.setCharacterEncoding("UTF-8");
 
  String action = request.getParameter("action");
 
  if("login".equals(action)){
  String account = request.getParameter("account");
  String password = request.getParameter("password");
 
  int timeout = new Integer(request.getParameter("timeout"));
  String ssid = calcMD5(account + KEY);
  Cookie accountCookie = new Cookie("account", account);
  accountCookie.setMaxAge(timeout);
  Cookie ssidcCookie = new Cookie("ssid", ssid);
  ssidcCookie.setMaxAge(timeout);
  response.addCookie(accountCookie);
  response.addCookie(ssidcCookie);
  response.sendRedirect(request.getRequestURI() + "?" + System.currentTimeMillis());
  return;  
  }
  else if ("logout".equals(action)){
  Cookie accountCookie = new Cookie("account", "");
  accountCookie.setMaxAge(0);
  Cookie ssidcCookie = new Cookie("ssid", "");
  ssidcCookie.setMaxAge(0);
  response.addCookie(accountCookie);
  response.addCookie(ssidcCookie);
  response.sendRedirect(request.getRequestURI() + "?" + System.currentTimeMillis());
  return;    
  }
  boolean login = false;
  String account = null;
  String ssid = null;
 
  if (request.getCookies() != null){
  for(Cookie cookie: request.getCookies()){
  if(cookie.getName().equals("account"))
  account = cookie.getValue();
  if(cookie.getName().equals("ssid"))
  ssid = cookie.getValue();
  }
 
  if(account != null && ssid != null){
  login = ssid.equals(calcMD5(account + KEY));
  }
%><legend><%=login ? "歡迎回來" : "請先登陸" %></legend>
<%if(login){ %>
歡迎你, ${cookie.account.value}. &nbsp;&nbsp; 
<a href="${pageContext.request.requestURI}?action=logout">注銷</a>
<%} else { %>
<form action="${pageContext.request.requestURI}?action=login" method="post">
<table>
<tr><td>帳號:</td>
<td><input type="text" name="account" style="width=200px; "/></td>
<tr><td>密碼:</td>
<td><input type="text" name="password" style="width=200px; "/></td>
</tr>
<tr>
<td>有效期:</td>
<td>
<input type="radio" name="timeout" value="-1" checked/>關閉瀏覽器失效<br/>
<input type="radio" name="timeout" value="<%=30*24*60*60 %>"/>30天內有效<br/>
<input type="radio" name="timeout" value="<%=Integer.MAX_VALUE %>"/>永久有效<br/>
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="登陸" class="button"/></td>
</tr>
</table>
</form>
<%} %>提示错误:
Multiple annotations found at this line:
- Syntax error on token "}", delete this token
- Syntax error, insert "}" to complete ClassBody
- Syntax error, insert "else Statement" to complete 
 IfStatement
- Syntax error, insert "}" to complete Block
但是我看了下语法没有问题啊,为什么会报错呢?求助!!!

解决方案 »

  1.   

    因为你前面的代码 有 {} 不匹配,初步目测是:  if (request.getCookies() != null){
       for(Cookie cookie: request.getCookies()){
       if(cookie.getName().equals("account"))
       account = cookie.getValue();
       if(cookie.getName().equals("ssid"))
       ssid = cookie.getValue();
       }
     
      if(account != null && ssid != null){
       login = ssid.equals(calcMD5(account + KEY));
      }
    %>
    这段话的第一个 if 没有 右括号
      

  2.   

    你看一下你的代码 最后一行有<%}%>
    之前你的那半边括号没有