登录跳转首页的功能

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
<h2>首页1</h2>
</body>
</html>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<h2>登录页</h2>
<form action="/my01_war/login" method="post">
    <div>
        账号:<input type="text" name="username"/>
    </div>
    <div>
        密码:<input type="text" name="password"/>
    </div>
    <input type="submit" />
</form>
</body>
</html>

package java0808;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username =req.getParameter("username");
        String password =  req.getParameter("password");
        System.out.println("账号为"+username);
        System.out.println("密码为"+password);


        if (username.equals("zs")&&password.equals("123")){
            HttpSession session = req.getSession();
            session.setAttribute("isLogin",true);
            resp.sendRedirect(req.getContextPath()+"/index.html");
        }
        else {
            resp.setContentType("text/plain;charset=UTF8");
            resp.getWriter().write("登陆失败");
            resp.sendRedirect(req.getContextPath()+"/login.html");
        }
    }
}

package java0808;

import org.jboss.weld.context.http.Http;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebFilter("/*")
public class MyFilter implements Filter {           //过滤器

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {//init指的是初始化
        System.out.println("过滤器的初始化");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        request.setCharacterEncoding("UTF8");
        System.out.println("经过过滤器要做的事情");
        HttpServletRequest httpServletRequest =(HttpServletRequest) request;
        HttpServletResponse httpServletResponse=(HttpServletResponse) response;


        String path=httpServletRequest.getServletPath();
        if ("/login.html".equals(path)||"/login".equals(path)){
            chain.doFilter(request, response);
            return;
        }

        HttpSession session = httpServletRequest.getSession();//获得session
        boolean isLogin = false;
        if (session.getAttribute("isLogin")!=null)
            isLogin=(Boolean)session.getAttribute("isLogin");

        if (isLogin) {
            chain.doFilter(request, response);
            return;
        }
         ((HttpServletResponse) response).sendRedirect(httpServletRequest.getContextPath()+"/login.html");
    }

    @Override
    public void destroy() {
        System.out.println("销毁");
    }
}
首先,你需要创建两个Activity,一个用于登录,一个用于注册。我们称之为LoginActivity和RegisterActivity。 在LoginActivity中,你需要添加一个“登录”按钮,当用户点击该按钮时,你需要验证用户输入的用户名和密码是否正确。如果验证通过,则跳转到主Activity,否则,显示错误消息。 在RegisterActivity中,你需要添加一个“注册”按钮,当用户点击该按钮时,你需要将用户输入的用户名和密码保存到本地数据库中。 接下来,你需要在LoginActivity和RegisterActivity中添加一些代码,以实现跳转功能。具体步骤如下: 1. 在LoginActivity中,添加以下代码: ``` // 跳转到注册页面 TextView registerTextView = findViewById(R.id.registerTextView); registerTextView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(LoginActivity.this, RegisterActivity.class); startActivity(intent); } }); ``` 2. 在RegisterActivity中,添加以下代码: ``` // 跳转登录页面 TextView loginTextView = findViewById(R.id.loginTextView); loginTextView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(RegisterActivity.this, LoginActivity.class); startActivity(intent); } }); ``` 现在,当用户点击“注册”按钮时,会跳转到RegisterActivity;当用户点击“登录”按钮时,会跳转到LoginActivity。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值