首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >amp什么意思啊_登录 Login

amp什么意思啊_登录 Login

作者头像
全栈程序员站长
发布2022-09-20 19:07:21
发布2022-09-20 19:07:21
7630
举报

大家好,又见面了,我是你们的朋友全栈君。

关于注册页面和登录页面的业务流程

form表单中确定action提交地址 method 确定提交的方法—》写出相对应的Servlet,假如接受的数据不多 ,那么用

String username = request.getParameter(“username”); String password = request.getParameter(“password”);接受账户和密码 假如数据多 那么需要工具类集合对数据进行处理 ,对于regist需要建立对应的数据库 和domain—>生成get 和set方法,便于插入数据,将这些数据进行转发,request.getRequestDispatcher(“/login.jsp”).forward(request, response);登录成功 跳转首页面response.sendRedirect(request.getContextPath());详细代码和效果图 如下

package cn.lijun.Demo

import java.io.IOException; import java.sql.SQLException;

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

import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler;

import com.ithiema.register.User; import com.ithiema.utils.DataSourceUtils;

public class LoginServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1 解决乱码 request.setCharacterEncoding(“UTF-8”);

//2、获得用户名和密码 String username = request.getParameter(“username”); String password = request.getParameter(“password”);

User login = null; try { login = login(username,password); } catch (SQLException e) { e.printStackTrace(); } //3、通过user是否为null判断用户名和密码是否正确 if(login!=null){ //用户名和密码正确 //登录成功 跳转到网站的首页 response.sendRedirect(request.getContextPath()); }else{ //4用户名或密码错误 //跳回当前login.jsp //使用转发 转发到login.jsp 向request域中存储错误信息 request.setAttribute(“loginInfo”, “用户名或密码错误”); //转发 request.getRequestDispatcher(“/login.jsp”).forward(request, response); }

}

public User login(String username,String password) throws SQLException{ QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource()); String sql = “select * from user where username=? and password=?”; //只有一个对象 User user = runner.query(sql, new BeanHandler<User>(User.class), username,password); return user; }

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/166939.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档