给 #2014Java与大数据# 指派了新任务。
MVC模式理解
1. 浅谈 你对MVC模式的理解
2. 编写一个简单的JavaWeb应用,描述MVC模式在JavaWeb开发中是怎么应用的。
给 #2014Java与大数据# 指派了新任务。
编写一个EL函数
•编写一个EL函数,接收两个int型参数,函数的返回结果是两参数的和
package controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class AddCartServlet
*/
@WebServlet("/AddCartServlet")
public class AddCartServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public AddCartServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//判断Cookie信息是否存在,存在直接添加到购物车,否则显示登录页面
String name=request.getParameter("name");
name=new String(name.getBytes("iso-8859-1"),"utf-8");
System.out.println("商品名:"+name);
HttpSession session=request.getSession();
List<String> cart=(List<String>) session.getAttribute("cart");
if(cart == null){
cart=new ArrayList<String>();
cart.add(name);
}else{
cart.add(name);
}
session.setAttribute("cart",cart);
response.sendRedirect("index.html");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
package controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class IndexServlet
*/
@WebServlet("/IndexServlet")
public class IndexServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public IndexServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
Cookie[] cookies = request.getCookies();
if(cookies!=null){
String name=null;
String pwd=null;
System.out.println("cookie !=null");
for(Cookie cookie:cookies){
System.out.println("name:"+cookie.getName());
System.out.println("value:"+cookie.getValue());
if(cookie.getName().equals("name")){
name=cookie.getValue();
}
if(cookie.getName().equals("pwd")){
pwd = cookie.getValue();
}
}
if(name.equals("admin")&&pwd.equals("1234")){
System.out.println("doget --cookie");
request.getRequestDispatcher("index.html").forward(request,response);
return;
}
}
response.encodeRedirectURL("login.html");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
package controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class LoginServlet
*/
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String name = request.getParameter("userName");
String pwd = request.getParameter("userPwd");
//访问数据库,查看用户是否存在
if(name!=null && name.equals("admin") && pwd!=null && pwd.equals("1234")){
String autoLogin = request.getParameter("autoLogin");
if(autoLogin!=null&&autoLogin.equals("on")){
//响应的处理,保存用户名密码到cookie中
System.out.println("autoLogin:"+autoLogin);
Cookie c1 = new Cookie("name",name);
Cookie c2 = new Cookie("pwd",pwd);
c1.setMaxAge(5*60);
c2.setMaxAge(5*60);
response.addCookie(c1);
response.addCookie(c2);
}
request.getRequestDispatcher("index.html").forward(request, response);
return;
}
response.sendRedirect("login.html");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
package controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class ShowCartServlet
*/
@WebServlet("/ShowCartServlet")
public class ShowCartServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ShowCartServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
HttpSession session = request.getSession();
List<String> cart = (List<String>) session.getAttribute("cart");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.write("<html><head><title>购物车</title></head><body><hr>");
for (int i = 0; i < cart.size(); i++)
{
if(i==cart.size()-1){
out.write("购物车中商品的数量为"+i);
break;
}
out.write("商品名:" + cart.get(i));
System.out.println("商品名:" + cart.get(i));
out.write("<hr>");
}
out.write("</body></html>");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>详细信息</title>
</head>
<body>
<table>
<tr>
<td>商品名</td> <td>商品A </td>
</tr>
<tr>
<td>商品描述</td> <td>男装</td>
</tr>
</table>
<a href="index.html">返回首页</a>
<a href="addCart?id=1&name=商品A">添加到购物车</a>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>详细信息</title>
</head>
<body>
<table>
<tr>
<td>商品名</td>
<td>商品B</td>
</tr>
<tr>
<td>商品描述</td>
<td>女装</td>
</tr>
</table>
<a href="index.html">返回首页</a>
<a href="addCart?id=2&name=商品B">添加到购物车</a>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>详细信息</title>
</head>
<body>
<table>
<tr>
<td>商品名</td>
<td>商品C</td>
</tr>
<tr>
<td>商品描述</td>
<td>童装</td>
</tr>
</table>
<a href="index.html">返回首页</a>
<a href="addCart?id=3&name=商品C">添加到购物车</a>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>商品列表</title>
</head>
<body>
<table>
<tr>
<td>商品名</td>
<td>价格</td>
<td colspan="2">操作</td>
</tr>
<tr>
<td>商品A</td>
<td>35</td>
<td><a href="detailA.html">查看详情</a></td>
<td><a href=addCart?id=1&name=商品A>添加到购物车</a></td>
</tr>
<tr>
<td>商品B</td>
<td>40</td>
<td><a href="detailB.html">查看详情</a></td>
<td><a href="addCart?id=2&name=商品B">添加到购物车</a></td>
</tr>
<tr>
<td>商品C</td>
<td>45</td>
<td><a href="detailC.html">查看详情</a></td>
<td><a href="addCart?id=3&name=商品C">添加到购物车</a></td>
</tr>
</table>
<a href="showCart">查看购物车</a>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="login" method="post">
<table border="1">
<tr>
<td width="100">用户名:</td>
<td><input type="text" name="userName"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="userPwd"></td>
</tr>
<tr>
<td><input type="reset" value="重置" /></td>
<td><input type="submit" value="登录" /> <input type="radio"
value="on" name="autoLogin" /> 自动登录</td>
</tr>
</table>
</form>
</body>
</html>
<?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" 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>shoppingcar</display-name>
<welcome-file-list>
<welcome-file>index.do</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>IndexServlet</servlet-name>
<servlet-class>controller.IndexServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>IndexServlet</servlet-name>
<url-pattern>/index.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>AddCartServlet</servlet-name>
<servlet-class>controller.AddCartServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AddCartServlet</servlet-name>
<url-pattern>/addCart</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ShowCartServlet</servlet-name>
<servlet-class>controller.ShowCartServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ShowCartServlet</servlet-name>
<url-pattern>/showCart</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>controller.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>
给 #2014Java与大数据# 指派了新任务。
使用HttpSession实现购物车功能
•应用程序:实现购物车功能
...
初学JavaWeb,请远离框架,从Servlet、Filter开始。
http://www.oschina.net/question/12_52027
123