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>
 
原文地址:https://www.cnblogs.com/tinaluo/p/10191688.html