MyEclipse结构如下:
在my包下有2个类,
一个是Servlet是LoginServlet.java
package my;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public LoginServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String user=request.getParameter("user");
String password=request.getParameter("password");
if("123456".equals(password)){
HttpSession hs=request.getSession();
hs.setAttribute("user", user);
//重定向到index.jsp
String contextPath=request.getContextPath();
response.sendRedirect(contextPath+"/index.jsp");
}
else{
response.sendError(400, "密码错误!");
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
另外一个是Test2Action.java
package my;
import com.opensymphony.xwork2.ActionSupport;
public class Test2Action extends ActionSupport
{
String reason = "A test message!";
int result = 0;
@Override
public String execute() throws Exception
{
result = 100;
return "success";
}
public String getReason()
{
return reason;
}
public void setReason(String reason)
{
this.reason = reason;
}
public int getResult()
{
return result;
}
public void setResult(int result)
{
this.result = result;
}
}
struts.xml配置如下
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="main" extends="struts-default">
<action name="Test2" class="my.Test2Action" >
<result name="success"> /Test2_success.jsp</result>
</action>
</package>
</struts>
index.jsp配置
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
首页
</body>
</html>
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form method="post" action="servlet/LoginServlet">
用户名:<input type="text" name="user"><br><br>
密码:<input type="password" name="password"><br><br>
<input type="submit" name="登录"><br><br>
</form>
</body>
</html>
Test2_success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'Hello.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1> 测试1 : #parameters </h1>
( 相当于 request.getParameter("") ) <br>
result: <s:property value="#parameters['check']" />
<h1> 测试2 : #session </h1>
( 相当于 session.getAttribute("") ) <br>
result: <s:property value="#session['user']" /> <br>
另外一种写法 : <s:property value="#session.user" />
<h1> 测试3 : #reqeust</h1>
#request['xxx'] 相当于 request.getAttribute("xxx") <br>
#parameters['xxx'] 相当于 request.getParameter("xxx") <br>
两者不同
<h1> 测试4 :#action </h1>
result: <s:property value="#action['reason']" /> <br>
相当于: <s:property value="reason" />
<h1> 测试5 :if判断 </h1>
<s:if test="result > 100" >
够大!
</s:if>
<s:else>
太小了!
</s:else>
<h1> 测试6 :if判断 </h1>
<s:if test="#session['user'] == null" >
未登录
</s:if>
<s:else>
<s:property value="#session['user']" />,您好!
</s:else>
</body>
</html>