java servlet实例_一个完整的简单jsp+servlet实例,实现简单的登录

1、先创建web project,项目名为RegisterSystem,

2、在WebRoot 目录下创建login.jsp文件:

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

My JSP 'login.jsp' starting page

This is my JSP page.

username:

password:

3、在scr目录下的com.ht.servlet编写AccountBean.java文件,代码如下:

package com.ht.servlet;

public class AccountBean {

private String username;

private String password;

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

}

4、在scr目录下的com.ht.servlet编写servlet类CheckAccount.java文件,代码如下:

package com.ht.servlet;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

public class CheckAccount extends HttpServlet {

@Override

protected void doPost(HttpServletRequest req, HttpServletResponse resp)

throws ServletException, IOException {

doGet(req,resp); }

@Override

public void doGet(HttpServletRequest req, HttpServletResponse resp)

throws ServletException, IOException {

HttpSession session = req.getSession();  AccountBean account = new AccountBean();  String username = req.getParameter("username");

String pwd = req.getParameter("pwd");  account.setPassword(pwd);

account.setUsername(username);  if((username != null)&&(username.trim().equals("jsp"))) {

if((pwd != null)&&(pwd.trim().equals("1"))) {

System.out.println("success");

session.setAttribute("account", account);    String login_suc = "success.jsp";

resp.sendRedirect(login_suc);    return;

}

}

String login_fail = "fail.jsp";

resp.sendRedirect(login_fail);  return;

}

}

5、在WebRoot目录下编写success.jsp文件 成功后跳转

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

My JSP 'success.jsp' starting page

AccountBean account = (AccountBean)session.getAttribute("account");

%>

username:

password:

basePath:

path:

6、在WebRoot目录下编写fail.jsp文件 失败后跳转

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

My JSP 'fail.jsp' starting page

Login Failed!

basePath:

path:

7、修改web.xml配置文件

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

login.jsp

This is the description of my J2EE component

This is the display name of my J2EE component

CheckAccount

com.ht.servlet.CheckAccount

CheckAccount

/login

输入测试路径:http://localhost:8080/RegisterSystem/login.jsp

注意:

.处理中文乱码问题

在Sertlet中加

response.setContentType("text/html;charset=utf-8")

在jsp页面中加

在servlet中

String username=request.getParameter("username");

username=new String(username.getBytes(“ISO-8859-1”),"UTF-8");

reference :http://blog.sina.com.cn/s/blog_5c5bc9070100z7wb.html

http://lelglin.iteye.com/blog/967503

http://zhidao.baidu.com/link?url=qg1AcsUjhkG4gHBv8GtHDjIQuwgtLto1_eyeinBx-b8TW9HAcYRR1zizL8vTDPTAjLwHPOdz7NcwJun-HyJXeK

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值