web第三节课作业

这节课实现了什么?

  1. User.java
    • 用户对象
  2. UserDao.java
    • 数据流
  3. UserServlet.java
    • 调用servlet
  4. web.xml
    • 配置文件
  5. index.jsp
    • 网页编辑

项目文件

这里写图片描述

1.User.java—>建立User对象 并配上set get hashcode

在User内编写private数据 username、password

并且设置set get hashcode equals 方法

代码如下:

package com.zking.bean;

public class User {

    private String username;
    private String password;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((password == null) ? 0 : password.hashCode());
        result = prime * result
                + ((username == null) ? 0 : username.hashCode());
        return result;
    }
    @Override

    //equals判断username and password 是否相同 相同的话就不能登录
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        User other = (User) obj;
        if (password == null) {
            if (other.password != null)
                return false;
        } else if (!password.equals(other.password))
            return false;
        if (username == null) {
            if (other.username != null)
                return false;
        } else if (!username.equals(other.username))
            return false;
        return true;
    }
}

2.UserDao.java

package com.zking.dao;

import com.zking.bean.User;

public class UserDao {
    public boolean login(User user){
        User u = new User();
        u.setUsername("123");
        u.setPassword("xiekun");
        //只有正确的输入用户名:123,密码:xiekun 才能登陆成功
        return u.equals(user);
    }
}

3.UserServlet.java–>调用servlet

package com.zking.action;


import com.zking.bean.User;
import com.zking.dao.UserDao;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class UserServlet extends HttpServlet{
    public void service (HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException{

        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        //获得URL地址
        StringBuffer str = request.getRequestURL();
        //out.println(str+"  ");      //out print ---->  http://localhost:8899/testweb/login.action

        //截取路径  path = login
        String path = str.substring(str.lastIndexOf("/")+1, str.lastIndexOf("."));
        //点击登录 即可输出path
        System.out.println(path);


        if("login".equals(path)){

            //username and password was writtern in username and pwd
            String username = request.getParameter("username");
            String pwd = request.getParameter("password");


            User u = new User();    
            u.setUsername(username);
            u.setPassword(pwd);
            UserDao ud = new UserDao();
            boolean flag = ud.login(u);

            if(flag){
                out.println("登陆成功!");
            }else{
                out.println("用户名或者密码错误!");
            }
        }
        else if("register".equals(path)){

        }else{
            out.println("bilibili");
        }
    }
}

4.web.xml—>配置servlet部分


  <servlet>
    <servlet-name>login</servlet-name>
    <servlet-class>com.zking.action.UserServlet</servlet-class>  
  </servlet>
  <servlet-mapping>
    <servlet-name>login</servlet-name>
    <url-pattern>/login.action</url-pattern>
  </servlet-mapping>

5. index.jsp–>网页显示部分


<html>
    <head>
        <title>login in</title>
    </head>

    <body>
          <form action="/testweb/login.action">
            <table>
                <tr align="center">
                    <td colspan ="4" style = "color:red;"><h1>login&nbsp;&nbsp;in</h1></td>
                </tr>
                <tr>
                    <td ><label for="uname">user:</label></td>
                    <td colspan ="3"><input typle ="text" id="uname" name="username" value="QQ/email/phone"></td>
                </tr>
                <tr>
                    <td ><label for="upassword">password:</label></td> 
                    <td colspan ="3"><input typle ="text" id="upassword" name="password" ></td>
                    <!-- td.name是框框的名字 td.id是与label的upassword匹配-->
                 </tr>
                <tr align="center">
                    <td colspan ="4"><input type="submit" value="login o"></td>
                </tr>
            </table>        
        </form> 
    </body>
</html>

这里写图片描述

新手学习中,如有错误,烦请斧正!

  1. http://localhost:8899/testweb 可以跳到登录界面
  2. http://localhost:8899/testweb/login.action 可以跳入点击后的界面,不过登录失败
  3. http://localhost:8899/testweb/login.action?username=123&password=xiekun 可以登陆成功
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值