zoukankan      html  css  js  c++  java
  • jsp的示例

    目录结构

    login.jsp

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>jsp测试页面</title>
    </head>
    <body>
        <form method="post" action="login">
            username:
            <input type="text" name="username"><br/>
            password:
            <input type="text" name="pwd"><br/>
            <input type="submit" value="提交">
        </form>
    </body>
    </html>

    hello.html

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>欢迎来到菜鸟营地</title>
    </head>
    <body>
        hello servlet
    </body>
    </html>

    FirstJsp.java

    public class FirstJsp extends HttpServlet {
        private static final long serialVersionUID = 1L;
        
        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            String name = req.getParameter("username");
            String pwd = req.getParameter("pwd");
            if((name != null)&&(name.trim().equals("jsp"))){
                if((pwd != null)&&(pwd.trim().equals("1"))){
                    
                    String login = "hello.html";
                    resp.sendRedirect(login);
                    return;
                }
            }
        }
        
        protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            this.doGet(req, resp);
        }
      }

    web.xml配置

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
      <servlet>
          <servlet-name>log</servlet-name>
          <servlet-class>FirstJsp</servlet-class>
      </servlet>
      <servlet-mapping>
          <servlet-name>log</servlet-name>
          <url-pattern>/login</url-pattern>
      </servlet-mapping>
    </web-app>

    输入访问地址

    http://localhost:8080/jsptest/login.jsp

    点击提交

    注意: 访问地址是直接访问的jsp 点击提交之后 servlet容器 调用servlet 根据from表单中action 去找web.xml的url地址

    然后去Java文件中运行处理方法  将处理结果返回给servlet 容器

  • 相关阅读:
    制作一款3D炸弹超人游戏
    C#集合中的Add与AddRange方法
    NGUI与EasyTouch结合使用
    Buff系统的实现
    Buff系统框架设计
    Buff系统设计
    Linux 服务管理两种方式service和systemctl
    centos上为新创建的用户(git)指定根目录并生成公钥和私钥
    centos7安装php7
    centos7上安装mysql8(下)
  • 原文地址:https://www.cnblogs.com/zjf6666/p/6565774.html
Copyright © 2011-2022 走看看