servlet之登录页面

<span style="font-size:18px;">首先建一个名为 LoginServlet的servlet:

import java.io.IOException;
import java.io.PrintWriter;

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

public class LoginServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public LoginServlet() {
		super();
	}

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String username=request.getParameter("uname");
		String password=request.getParameter("upwd");
		
		System.out.println("用户名 ==》"+username);
		System.out.println("密码 ==》"+password);
		
	}

}

然后在index.jsp中加入表单:
<pre name="code" class="html"><%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>登录页面</title>
  </head>
  
  <body>
<form action="<%= request.getContextPath() %>/LoginServlet" method="post">
userName:<input type="text" name="uname" /><br />
password:<input type="password" name="upwd" /><br />
<input type="submit" value="Login" />
<input type="submit" value="Reset" />
</form>
  </body>
</html>

最后在web.xml中配置路径:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	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">
  
  
 
  <servlet>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>com.jikexueyuan.LoginServlet.LoginServlet</servlet-class>
  </servlet>

<servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/LoginServlet</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

只要在浏览器中访问路径http://127.0.0.1:8080/Login2/index.jsp就可以了,不过这只是响应了登录的请求,没有登录的效果,经过优化后可以实现用户名和密码的匹配登录和登录跳转的页面:

优化后的LoginServlet为:

package com.jikexueyuan.LoginServlet;

import java.io.IOException;
import java.io.PrintWriter;

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

public class LoginServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public LoginServlet() {
		super();
	}

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String username=request.getParameter("uname");
		String password=request.getParameter("upwd");
		
		System.out.println("用户名 ==》"+username);
		System.out.println("密码 ==》"+password);
		
		String forward=null;
		//跳转页面:
		if(username.equals("panpan")&&password.equals("963524")){
			//请求转发
			//forward="/success.jsp";
			forward="http://www.jikexueyuan.com";
			//RequestDispatcher rd=request.getRequestDispatcher(forward);
			//rd.forward(request, response);
			//请求重定向
			response.sendRedirect(forward);
		}else{
			//请求转发
			//forward="/false.jsp";
			//RequestDispatcher rd=request.getRequestDispatcher(forward);
			//rd.forward(request, response);
			//请求重定向
			response.sendRedirect(request.getContextPath()+"/false.jsp");
		}
	}

}

添加一个登录成功的界面success.jsp:
<pre name="code" class="html"><%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>登录成功</title>
    
  </head>
  
  <body>
   登录成功,啦啦啦啦 <br>
 你提交的信息为:<br>
 用户名:<%=request.getParameter("uname") %><br>
 密码:    <%=request.getParameter("upwd") %><br>
 <a href="index.jsp">返回登录页面</a>
   
  </body>
</html>

再添加一个登录失败的页面false.jsp:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>登录失败</title>
    
	
  </head>
  
  <body>
    抱歉,您的页面登录失败! <br>
    请检查您的信息:<br>
    用户名:<%=request.getParameter("uname") %><br>
    密码:    <%=request.getParameter("upwd") %><br>
 <a href="index.jsp">返回登录页面</a>
  </body>
</html>

登录效果:


 
 



                
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值