标签:
本来网站都是一个个静态HTML的,但很快我们就不满足于这样了。动态网页应运而生。out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n");
out.write("<html>\r\n");
out.write("<head>\r\n");
out.write("<title>首页</title>\r\n");
out.write("</head>\r\n");
out.write("<body>\r\n");
out.write("Hello, " + new Date().toLocalString
out.write("</body>\r\n");
out.write("</html>\r\n");
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>首页</title>
</head>
<body>
<ss:a label="首页" href="/index.jsp" />
<ss:a label="登入" href="/login.jsp" />
<ss:a label="注册" href="/register.jsp" />
<ss:a label="登出" href="/logout.do" />
<hr />
<jsp:include page="/include/jsp/datetime.jsp" />
<%
String user = (String) session.getAttribute("user");
if (user != null && user.length() > 0) {
out.println("Welcome, <b>" + user + "</b>! You are logged in.");
}
%>
</body>
</html>
//一般采取这种办法
//在Servlet中将结果设置到request对象当中。
request.setAttribute("result", result);
//在JSP中通过request对象来获取。
<% Object result = request.getAttribute("result"); %>
<table>
<th>
<td>序号</td>
<td>姓名</td>
<td>性别</td>
<td>年龄</td>
<td>电话</td>
</th>
<c:forEach var="item" items="${request.list}">
<tr>
<td>${item.id}</td>
<td>${item.name}</td>
<td>${item.sex}</td>
<td>${item.age}</td>
<td>${item.phone}</td>
</tr>
</c:forEach>
</table>
//首先,你需要在Web.xml中进行配置
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.tee.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/Login</url-pattern>
</servlet-mapping>
//然后,需要编写一个继承自HttpServlet类的Servlet。
package com.tee.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class RegisterServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
String username = request.getParameter("username");
String password = request.getParameter("password");
if (UserDao.validate(username, password)) {
request.getSession.setAttribute("user", username);
response.setContentType("text/html; charset=utf-8");
response.sendRedirect("/index.jsp");
} else {
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher("/login.jsp");
request.setAttribute("errMsg", "login failure");
rd.forward(request, response);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException {
doGet(request,response);
}
}
这里引用“四刘”的回答:MVC框架实现了MVC模式。什么意思?
框架和普通库的几点区别里,我认为最重要的一点是控制反转。
框架规定了开发者写哪些代码/不写哪些代码,怎么写代码——这就是框架主要解决的问题。
标签:
原文地址:http://www.cnblogs.com/doit8791/p/4211936.html