JavaWeb课后练习

Posted MyNorth

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaWeb课后练习相关的知识,希望对你有一定的参考价值。

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

 

一个Servlet,一个xml文件,还有三个jsp页面实现简单的功能。

 

package P45_2;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
          String use = request.getParameter("user");//获取文本框内容:用户名
          String password = request.getParameter("password");//获取文本框内容:密码
          
          if (use.equals("caterpillar") && password.equals("123456")) {
              request.getRequestDispatcher("/Success.jsp").forward(request,response);//跳转页面
          }else {
              request.getRequestDispatcher("/Failure.jsp").forward(request,response);
          }
      }
}
LoginServlet.java

 

<?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>P45_2</display-name> 
  <servlet>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>P45_2.LoginServlet</servlet-class>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
</web-app>
web.xml

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
  <head>
    <title>登录失败</title>
  </head>
  
  <body>
    <h1>登录失败</h1>
    <a href = "login.jsp">点击返回</a>
    
  </body>
</html>
Failure.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <title>登录界面</title>
</head>
  
<body>
    <form action="LoginServlet" method = "post">
        <p>请输入用户名和密码<p>
         用户名:<input type="text" name = "user" >
         密码:<input type = "password"  name = "password">
        <input type = "submit" value = "登录">
    </form>
</body>
</html>
Login.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
  <head>
    <title>登录成功</title>
  </head>
  
  <body>
    <h1>登录成功!!!</h1>
  </body>
</html>
Success.jsp

 

 

 

 

以上是关于JavaWeb课后练习的主要内容,如果未能解决你的问题,请参考以下文章

吴恩达 MachineLearning 第五周课后练习代码

SSH:如何学

传智播客Javaweb程序设计任务教程 黑马程序员 课后答案合集

Java课后练习3

文件操作课后练习

Java课后练习6