struts2入门:登录的小例子,用户名和密码指定

登录页面login.jsp代码:
<%@ page language="java" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<form action="login.action" method="post">
	username:<input type="text"	name="username"/> 
	password:<input type="password" name="password"/>
	<input type="submit" value="submit"/>
</form>
</body>
</html>

登录成功页面success.jsp代码:
<%@ page language="java"  pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
    <body>
    欢迎${requestScope.username}登陆
  </body>
</html>

登录失败页面fail.jsp代码:
<%@ page language="java" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
  <body>
  用户名或密码错误 <br>
  </body>
</html>


action代码:
package com.lhtwqh.action;

public class LoginAction {
	private String username;
	private String password;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String execute() throws Exception {
		if (this.username.equals("admin") && this.password.equals("admin")) {
			return "success";
		} else {
			return "fail";
		}
	}
}


web.xml配置代码:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>
			org.apache.struts2.dispatcher.FilterDispatcher
		</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>


struts2配置文件struts.xml配置代码:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
   <package name="struts2" extends="struts-default">
	<action name="login" class="com.lhtwqh.action.LoginAction">
	    <result name="success">/success.jsp</result>
	    <result name="fail">/fail.jsp</result>
	</action>
   </package>
</struts>

你可能感兴趣的:(java,apache,jsp,xml,struts)