jsp+servlet实例
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
jsp+servlet实例
开发环境myeclipse+tomcat6
1、先创建web project,项目名为RegisterSystem,
2、在WebRoot 目录下创建login.jsp文件,只需修改body中的内容,如下所示:
3、在scr目录下的com.ht.servlet编写AcountBean.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) {
ername = 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文件成功后跳转
<%
AccountBean account = (AccountBean)session.getAttribute("account"); %>
username:<%= account.getUsername()%>
password:<%= account.getPassword() %>
6、在WebRoot目录下编写fail.jsp文件失败后跳转
Login Failed!
7、修改web.xml配置文件
xmlns="/xml/ns/javaee" xmlns:xsi="/2001/XMLSchema-instance" xsi:schemaLocation="/xml/ns/javaee /xml/ns/javaee/web-app_2_5.xsd">