servlet网页

 

 

 

编写一个应用程序,可以让用户在窗体网页上输入名称、密码,若名称为caterpillar且密码为123456,则显示一个HTML页面响应并有“登录成功”字样,否则显示“登录失败”字样,并由一个超链接连回窗体网页。注意:不可在地址栏上出现用户输入的名称、密码。

 

制作思路:

1.首先写一个html输出登录界面;风格设置mothod = “post”保证注意项目;

2.写一个servlet,用来获取页面内容,并加以判断,然后跳转页面;

3.写两个jsp,分别显示两个结果

 

制作总结:

1.服务器会自己创建请求request对象

2.在web.xml中的list为执行页面顺序,页面之间跳转应用action属性

3.对于用户名或密码为空的情况处理,使用js,alert提示或者p标签内容改变提示。

 

package practice;
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 get extends HttpServlet {
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
 }
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  String use = request.getParameter("tuser");//获取文本框内容:用户名
  String password = request.getParameter("tpw");//获取文本框内容:密码
  if (use.equals("caterpillar") && password.equals("123456")) {
   request.getRequestDispatcher("/JspSuccess.jsp").forward(request,
     response);//跳转页面
  } else {
   request.getRequestDispatcher("/JspFailure.jsp").forward(request,
     response);
  }
  }
 }
}

 

<?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">
  <display-name>practice</display-name> 
  
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>get</servlet-name>
    <servlet-class>practice.get</servlet-class>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>get</servlet-name>
    <url-pattern>/servlet/get</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
   <welcome-file>index.html</welcome-file>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>index.html</title>
 
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
  </head>
  
  <body>
   <form action="servlet/get" method = "post" onsubmit = "return isnull()">
   <p>请输入用户名和密码<p>
    用户名:<input type = "text"  name = "tuser"  id = "usr"></input><br>
    密 码:<input type = "password"  name = "tpw" id = "pw"></input>
    <input type = "submit" value = "登录">
   </form>

   <script type="text/javascript">//js:验证用户名或密码是否为空
    function isnull(){
     var user01 = document.getElementById("usr").value;
     var pw01 = document.getElementById("pw").value;
     if(user01 == "" ||pw01 == "")
     {
      alert("用户名或密码为空,请输入!");
      return false;
     }
     return true;
     /* var p = document.getElementsByTagName("p");
     if(user01 == "" ||pw01 == ""){
      p[0].innerHTML = "用户名或密码为空,请输入!";
      return false;
     }
     return true; */
    }
   </script>
  </body>
</html>

 

<%@ page language="java" import="java.util.*" pageEncoding="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>My JSP 'JspFailure.jsp' starting page</title>
    
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">    
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
  </head>
  
  <body>
    <font face = "楷体" size = "5" >登录失败!</font>
    <a href = "index.html">点击返回</a>
  </body>
</html>

 

<%@ page language="java" import="java.util.*" pageEncoding="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>My JSP 'JspSuccess.jsp' starting page</title>
    
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">    
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
  </head>
  
  <body>
    <font face = "楷体" size = "5" >登录成功!</font>
    <a href = "www.baidu.com">点击进入</a>
  </body>
</html>

 

 

 

 

 

 

 

 

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值