JSP动作学习一

创建一个简单的模拟登陆功能的网页,没有用数据库技术,只是简单的学习jsp语法

首先创建一个登陆页面

<%@ page language = "java" import="java.util.*" pageEncoding="utf-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  
  <body>
   <form action = "judge.jsp">
        name<input type = "text" name = "name">
        password<input type = "password" name = "password">
        <input type = "submit" value = "提交">
    </form>
  </body>
</html>


然后创建一个判断用户名和密码的jsp文件,这里默认用cheng做用户名,song做密码

<%@ page language = "java" pageEncoding="utf-8" %>
<html>
    <head>
        <title>forward 动作</title>
    </head>
    <body>
    <jsp:useBean id="tb" scope = "request" class ="bean.testbean"></jsp:useBean>
    <jsp:setProperty property="*" name="tb"/>
    <%-- <%@include file = "jisuan.jsp" %> --%>
        <%
            String name = tb.getName();
            String password = tb.getPassword();
             if(name.equals("cheng")&&password.equals("song")){
         %>
         <jsp:forward page="success.jsp"></jsp:forward>
         <%
         }else{
          %>
          <jsp:forward page="fail.jsp"></jsp:forward>
          <%
              }
           %>
    </body>
</html>
 
  
 
 

在判断之后,根据判断结果进入两个网页,一个是成功登陆,一个是登录失败

<%@ page language = "java" pageEncoding="utf-8" %>
<html>
    <head>
        <title>登陆成功</title>
    </head>
    <body>
        <jsp:useBean id="tb" scope = "request" class = "bean.testbean"></jsp:useBean>
        登陆成功
        <hr>
        你的用户名是:<jsp:getProperty property="name" name="tb"/></br>
        你输入的密码是:<jsp:getProperty property="password" name="tb"/>
    </body>
</html>
<%@ page language = "java" pageEncoding="utf-8" %>
<html>
    <head>
        <title>登录失败</title>
    </head>
    <body>
    <jsp:useBean id="tb" scope = "request" class = "bean.testbean"></jsp:useBean>
        登录失败
        你输入的用户名<jsp:getProperty property="name" name="tb"/>错误</br>
        你输入的密码:<jsp:getProperty property="password" name="tb"/>
    </body>
</html>

其中的Javabean如下

package bean;

public class testbean {
    public String name ;
    public String password;
    public testbean(){}/*该处的public开始时掉了,导致后来一直报错,调试半天,切记切记,当程序说找不到bean文件时,一定要看看是不是忘了public*/
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    
}

 

你可能感兴趣的:(JSP动作学习一)