2013年9月12日 星期四

stil-choose改寫if..else法

choose內的when.....otherwise類似於script的if...else 。

原有程式
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");

if (username.equals("abc") && password.equals("123"))
{
    session.setAttribute("login", "ok");
    response.sendRedirect("ok.jsp");
}
else
{
    response.sendRedirect("fail.jsp");
}
%>



 可改寫為
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set value="OK" var="login" scope="session"/>
<c:choose>
   <c:when test="${param.username != '' && param.password !=''}">
    <c:redirect url="ok.jsp"/>
   </c:when>
   <c:otherwise>
   <c:redirect url="fail.jsp"/>
   </c:otherwise>
</c:choose>

沒有留言:

張貼留言