zoukankan      html  css  js  c++  java
  • JAVA EE:第一个例子(登录)

    名称 代码 说明
    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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        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">
        <display-name>hello</display-name>
        <welcome-file-list>
            <welcome-file>index.html</welcome-file>
            <welcome-file>index.htm</welcome-file>
            <welcome-file>index.jsp</welcome-file>
            <welcome-file>default.html</welcome-file>
            <welcome-file>default.htm</welcome-file>
            <welcome-file>default.jsp</welcome-file>
        </welcome-file-list>
        <servlet>
            <servlet-name>dispatcher</servlet-name>
            <servlet-class>mypack.DispatcherServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>dispatcher</servlet-name>
            <url-pattern>/dispatcher</url-pattern>
        </servlet-mapping>
    </web-app>
     
    servlet类
    package mypack;
    import javax.servlet.*;
    import java.io.*;
    
    public class DispatcherServlet extends GenericServlet
    {
        private static final long serialVersionUID = -927664542385240618L;
        private String target="/hello.jsp";
        public void service(ServletRequest request,ServletResponse response)throws ServletException,IOException{
            String username=request.getParameter("username");
            String password=request.getParameter("password");
            request.setAttribute("USER", username);
            request.setAttribute("PASSWORD", password);
            
            ServletContext context=getServletContext();
            RequestDispatcher dispatcher=context.getRequestDispatcher(target);
            dispatcher.forward(request, response);        
        }
    }
     
    login.html
    <!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>Insert title here</title>
    </head>
    <body>
        <form name="loginForm" method="POST" action="dispatcher">
            <table>
                <tr>
                    <td><div align="right">User Name:</div></td>
                    <td><input type="text" name="username"></td>
                </tr>
                <tr>
                    <td><div align="right">password</div></td>
                    <td><input type="text" name="password"></td>
                </tr>
                <tr>
                    <td><input type="submit" name="submit" value="submit"></td>
                    <td><input type="reset" name="reset" value="reset"></td>
                </tr>
            </table>
        </form>
    </body>
    </html>
     
    hello.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>Insert title here</title>
    </head>
    <body>
        hello:<%=request.getAttribute("USER")%>
    </body>
    </html>
     
  • 相关阅读:
    MyBatis与spring面试题-转载
    122. 买卖股票的最佳时机 II(贪心策略)
    121. 买卖股票的最佳时机
    120. 三角形最小路径和
    236. 二叉树的最近公共祖先(快手面试)
    b,b+树区别
    119. 杨辉三角 II
    118. 杨辉三角
    检查型异常(Checked Exception)与非检查型异常(Unchecked Exception)
    Redis
  • 原文地址:https://www.cnblogs.com/tinaluo/p/10191688.html
Copyright © 2011-2022 走看看