登陆示例(利用jsp,servlet)不连接数据库
首先是项目的目录结构先对web.xml文件做处理servlet_studyLoginServletcom.scup.servlet.LoginServletLoginServlet/LoginServletindex.htmlLogin.jsp(登录页)<%@ page
·
首先是项目的目录结构
先对web.xml文件做处理
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>servlet_study</display-name>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.scup.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
Login.jsp(登录页)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录页面</title>
</head>
<body>
//getContextPath()是对提交至处理文件的路径处理,提交方式为post
<form action="<%=request.getContextPath() %>/LoginServlet" method="post">
username:<input type="text" name="username" /><br />
password:<input type="password" name="password" /><br />
<input type="submit" value="登录" />
<input type="reset" value="重置" />
</form>
</body>
</html>
success.jsp(登录成功)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>成功登录</title>
</head>
<body>
<h1>登录成功</h1>
username:<%=request.getParameter("username") %><br />
password:<%=request.getParameter("password") %><br />
<a href="Login.jsp">返回登录页面</a>
</body>
</html>
error.jsp(登录失败)
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登陆失败</title>
</head>
<body>
<h1>登录失败</h1>
username:<%=request.getParameter("username") %><br />
password:<%=request.getParameter("password") %><br />
<a href="Login.jsp">返回登录页面</a>
</body>
</html>
LoginServlet(请求响应处理)
继承HttpServlet父类
package com.scup.servlet;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sun.xml.internal.ws.client.RequestContext;
public class LoginServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = -4875351145322914247L;
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
String username=req.getParameter("username");
String password=req.getParameter("password");
System.out.println("用户名:"+username);
System.out.println("密码:"+password);
super.service(req, resp);
}
}
使用doGet或doPOST方法实现信息的提交获取
package com.scup.servlet;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sun.xml.internal.ws.client.RequestContext;
public class LoginServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = -4875351145322914247L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.print("进入doget方法");
System.out.print("(调用dopost方法)");
//调用dopost方法
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("进入dopost方法");
String username=req.getParameter("username");
String password=req.getParameter("password");
System.out.println("用户名:"+username);
System.out.println("密码:"+password);
String forward=null;
if (username.equals("scup")&&password.equals("1122")) {
//请求转发
//由于sendRedirect方法不能将提交信息保存,所以使用forward标记(原方法见图2)
forward="http://www.baidu.com";
//RequestDispatcher(请求调度器,转发给forward地址)
//RequestDispatcher rd=req.getRequestDispatcher(forward);
//rd.forward(req, resp);
//由于forWord的内容(百度首页)不在同一应用目录,所以req.getContextPath()+"/01/success.jsp"这种方法会报404错误
//请求重定向
resp.sendRedirect(forward);
}else{
forward="/01/error.jsp";
RequestDispatcher rd=req.getRequestDispatcher(forward);
rd.forward(req, resp);
//请求重定向
//resp.sendRedirect(req.getContextPath()+"/01/error.jsp");
}
}
}
图2
各位需要对http 的post,get方法熟悉,了解post与get提交的区别,重定向方法无法获得表单提交的数据。
更多推荐
已为社区贡献1条内容
所有评论(0)