Regist&Login

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

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://www.cnblogs.com/lijun6/p/10473470.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,您的问题是关于如何基于node.js建立服务器,用于浏览器前端向服务器发请求,服务器根据请求的路径,将对应文件的内容响应给浏览器,并且实现login、regist、index、404等页面的访问路由,是吗? 首先,我们需要安装Node.js环境,然后在命令行中安装相关依赖库,如下所示: ``` npm install http fs path url ``` 接着,我们可以创建一个server.js文件,用于启动服务器并监听客户端请求,如下所示: ```javascript const http = require('http'); const fs = require('fs'); const path = require('path'); const url = require('url'); const server = http.createServer((req, res) => { // 解析请求路径 const pathname = url.parse(req.url).pathname; // 根据请求路径,读取对应文件 let filePath = path.join(__dirname, 'public', pathname === '/' ? 'index.html' : pathname); fs.readFile(filePath, (err, data) => { if (err) { // 处理文件不存在的情况 if (err.code === 'ENOENT') { res.writeHead(404, { 'Content-Type': 'text/html' }); res.end('<h1>404 Not Found</h1>'); } else { res.writeHead(500, { 'Content-Type': 'text/html' }); res.end(`<h1>500 Internal Server Error</h1><p>${err}</p>`); } } else { res.writeHead(200, { 'Content-Type': getContentType(filePath) }); res.end(data); } }); }); // 启动服务器 const port = process.env.PORT || 3000; server.listen(port, () => { console.log(`Server running on port ${port}`); }); // 根据文件扩展名,返回对应的Content-Type function getContentType(filePath) { const extname = path.extname(filePath); switch (extname) { case '.html': return 'text/html'; case '.css': return 'text/css'; case '.js': return 'text/javascript'; case '.json': return 'application/json'; case '.png': return 'image/png'; case '.jpg': return 'image/jpg'; default: return 'text/plain'; } } ``` 在上述代码中,我们启动了一个HTTP服务器,并监听客户端请求。当客户端发起请求时,我们根据请求路径,读取对应的文件并将其响应给客户端。如果文件不存在,我们则会返回404 Not Found的错误页面。 接着,我们可以在public文件夹下创建login、regist、index等页面的HTML文件,如下所示: ``` public |-- login.html |-- regist.html |-- index.html ``` 这样,当客户端向服务器发起/login请求时,我们就会读取public/login.html文件并将其返回给客户端了。 最后,我们还可以添加一个404.html文件作为404错误页面,如下所示: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>404 Not Found</title> </head> <body> <h1>404 Not Found</h1> </body> </html> ``` 当客户端请求不存在的路径时,我们就会返回404.html页面。 希望这个回答能够回答您的问题,如有不清楚的地方,欢迎继续提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值