javaEE笔记 管理员登陆

web目录下的index.jsp:

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2017/12/25/025
  Time: 20:40
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  <form >
    <br> 请输入管理员密码
    <input type="password" name="password">
    <input type="submit">
  </form>
  </body>
</html>

下面创建一个servlet,类名为Dologin,步骤如图
这里写图片描述
然后是这样
这里写图片描述
(注意,最下方那个Create javaEE6 annotated class不要勾选)
用jsp,servlet,javabean实现MVC:
jsp是试图V,servlet是控制器C,javabean是模型M
创建完之后,我们发现这样的问题。servlet都是红色的,找不到servlet
这里写图片描述

网上其他方法都是错误的。。只要我的才是正确的
file—>project structer
然后按下图
在项目中配置servlet

然后选中tomecat的lib文件夹下的servlet-api.jar,ok
这里写图片描述
好,现在servlet也配置好了
我们下面要做的东西是,在index.jsp页面上实现管理员登陆,把密码传到名为Dologin的servlet中,如果密码正确,跳转到addSubject.jsp,如果密码错误,则返回index.jsp

index.jsp:

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2017/12/25/025
  Time: 20:40
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  <form method="post" action="/admin">
    <br> 请输入管理员密码
    <input type="password" name="password">
    <input type="submit">
  </form>
  </body>
</html>

Dologin.java:

package servlet;

import java.io.IOException;

public class Dologin extends javax.servlet.http.HttpServlet {
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        String password=request.getParameter("password");
        if(password.equals("123456"))
            response.sendRedirect("/addSubject.jsp");
        else
            response.sendRedirect("/index.jsp");
    }

    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {

    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <servlet>
        <servlet-name>Dologin</servlet-name>
        <servlet-class>servlet.Dologin</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Dologin</servlet-name>
        <url-pattern>/admin</url-pattern>
    </servlet-mapping>
</web-app>

我们可以看到,在index.jsp中,通过

action="/admin"

找到了Dologin这个servlet
servlet中的url-pattern有三种配置方式
1.目录匹配,2.扩展名匹配,3.完全路径匹配

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值