Javaweb简易登录页面,提交用户名、密码,若用户名不为空字符串且长度在6-8位间,密码不为,空长度为6位,则登录成功,显示“**(用户名)登录成功!“,否则显示“***登录失败“,再加上所在专业.

第一个文件Test001.html是制作登陆界面action是用于跳转到Test001.java进行判断,具体每行代码的作用已经批注到代码旁边。

第二个文件Test001.java是用于判断,具体每行代码的作用已经批注到代码旁边。

第三文件是success.java成功界面,具体每行代码的作用已经批注到代码旁边。

第四个文件是fail.jsp失败界面,具体每行代码的作用已经批注到代码旁边。

第一个文件是HTML第二,三文件是Servlet第四个文件是Jsp

1.Test001.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册邮箱</title>
</head>
<body>
 <form action="test001"method="post">
  您的姓名:<input name="username" type = "text" ><br>
  您的密码:<input name="userpwd" type = "password"><br>
  您的学号:<input name="userid" type = "text" ><br>
  您的专业:<select name="usermajor" type = "major"> 
<option>---请选择---</option>
<option>软件工程</option>
<option>计算机网络</option>
<option>网络工程</option>
</select>
  <INPUT TYPE = "submit" value ="注册" name="submit">
  <INPUT TYPE = "reset" value ="重置" name="reset">
 </form>
</body>
</html>

2.success.java

package cc.openhome;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Test001
 */
@WebServlet("/test001")   
public class Test001 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Test001() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("UTF-8");//处理中文乱码
		//response.setContentType("text/html;charset=UTF-8");//处理中文乱码
		
		String name = request.getParameter("username");    //取得请求参数,取得username变量
		String pwd = request.getParameter("userpwd");    //取得请求参数,取得userpwd变量
		String major = request.getParameter("usermajor"); //取得请求参数,取得usermajor变量
		String id = request.getParameter("userid"); //取得请求参数,取得userid变量
		PrintWriter out = response.getWriter();
		
		  
		/*登录成功*/
		  if((name!="")&&(pwd!="")&&(name.length() >=6 && name.length() <=8)&&(pwd.length() == 6)) {
			  //验证用户名密码是否正确,名字不为空,密码不为空,名字的长度大于等于6小于等于8,密码的长度必须等于6  
			   request.getRequestDispatcher("success")//判断成功跳转到success
			   .forward(request, response);}
			  else 
			   response.sendRedirect("fail.jsp");//判断成功跳转到fail
			  }
			  
			 
		


	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	 }
	
}

3.success.java

package cc.openhome;

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

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

/**
 * Servlet implementation class success
 */
@WebServlet("/success")
public class success extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public success() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("utf-8");
		String yourName=request.getParameter("username");//取得请求参数,取得username变量
		String yourMajor = request.getParameter("usermajor");  //取得请求参数,取得usermajor变量
		String yourId=request.getParameter("userid");//取得请求参数,取得userid变量
		PrintWriter out=response.getWriter();
		out.println("<body><html>");//<html>称之为开始标签,页面是由一对标签组成
		out.println("<p> Hello"+yourName+"登陆成功</p >");//调用取得请求的参数yourName
		out.println("<p> 你是"+yourMajor+"专业</p >");//调用取得请求的参数yourMajor
		out.println("<p> 你的学号是"+yourId+"</p >");//调用取得请求的参数youId
		out.println("</body></html>");//</html>称之为结束标签
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

4.fail.jsp

​

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body bgcolor=cyan>
<%request.setCharacterEncoding("utf-8");
out.println("<p> 对不起您登陆失败</p >");//打印对不起您登陆失败
%>
</body>
</html>

[点击并拖拽以移动]
​

运行截图

1.登录界面

 2.成功界面

3.失败界面:用户名或密码为空或用户名不大于等于六位小于等于八位也是返回失败界面或者密码不是六位数也是返回失败界面。

 

  • 12
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Lhhhyyy.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值