首先在WEB-INF 建一个web.xml 配置文件 用于配置监听、过滤、servlet、设置会话(session)过期时间
启动web项目后,web容器首先回去找web.xml文件,读取这个文件
容器会创建一个 ServletContext ( servlet 上下文),整个 web 项目的所有部分都将共享这个上下文。ServletContext存储于服务端,每个客户端都可以访问到,数据共享
容器将 转换为键值对,并交给 servletContext
容器创建 中的类实例,创建监听器
容器加载filter,创建过滤器, 要注意对应的filter-mapping一定要放在filter的后面
容器加载servlet,加载顺序按照 Load-on-startup 来执行
完整加载顺序:ServletContext -> context-param -> listener-> filter -> servlet
这里以MainSvl为例 主要是把svl名字还有路径写对
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>MainSvl</servlet-name>
<servlet-class>com.ice.control.MainSvl</servlet-class>
</servlet>
然后我们就可以写我们的main.jsp 和MainSvl了
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
AdmBiz biz =new AdmBiz();
try {
List<Fod> fods = biz.getRedisFod();
request.setAttribute("fods", fods);
request.getRequestDispatcher("/view/main.jsp").forward(request,response);
} catch (Exception e) {
request.getRequestDispatcher("/view/error.jsp").forward(request, response);
}
}
1.在使用表单提交数据到服务器的时候有两张方式可共选择,一个是post一个是get。可在<form>中的method属性中指定提交的方式。如:<form action="inputForm"method="get">,如果不指定method属性,则会默认该属性为”get”方式。通过get方式提交的数据有大小的限制,通常在1024字节左右。也就是说如果提交的数据很大,用get方法就可需要小心;而post方式没有数据大小的限制,理论上传送多少数据都可以。通过get传递数据,实际上是将传递的数据按照”key,value”的方式跟在URL的后面来达到传送的目的的 例如 password=123456;而post传递数据是通过http请求的附件进行的,在URL中并没有明文显示。
具体的我是通过监听先把所有的产品给缓存到redis中
我是通过监听先把所有的产品给缓存到redis中
具体的内容其实就是k,v结构存入redis
public void contextInitialized(ServletContextEvent arg0) {
List<Fod> fods =getAllFod();//获取食物
setRedisInfo(fods); //食物存入jedis
System.out.println("redis初始化首页餐品数据成功");
}
private List<Fod> getAllFod(){
List<Fod> fods= null;
AdmBiz biz =new AdmBiz();
try {
fods =biz.getFod();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return fods;
}
private void setRedisInfo(List<Fod> fods){
Jedis jedis = RedisUtil.getJedis();
for(Fod fd : fods){
String fno = String.valueOf(fd.getFno()); //存入jedis的是hash key value 结构 key 是主键fno 放一个字符串类型
Gson gson = new Gson();
String fdJson = gson.toJson(fd); //value是类 转化为json的字符串
jedis.hset("allFodList", fno, fdJson); // 存入redis 的 名字是 allFodList
}
}
然后redis 在转化回去
public List<Fod> getRedisFod() throws Exception{
List<Fod> fods =null;
Jedis jedis = RedisUtil.getJedis();
Map<String, String> maps = jedis.hgetAll("allFodList"); //从此方法获取个hashset 通过key名字
Set<String> sets = maps.keySet();
Gson gson = new Gson();
fods = new ArrayList<Fod>(10);
for(String key: sets){
String fdJson = maps.get(key);
Fod fd = gson.fromJson(fdJson, Fod.class); //从json转化橙food类
fods.add(fd);
}
return fods;
}
同时我们可以在main.jsp跳转到其它的servlet 例如
<a title="查看美食" href="<%=basePath%>ClassifySvl?styno=1">美食</a>
这样一次点击就是一次跳转。这个我们可以在后台通过
request.getParameter("styno") 来获取
然后到数据库进行查询
传统jdbc查询方式
public List<Fod> getClzFod(int a) throws Exception {
List<Fod> fods =null;
String sql ="select fno,fname,fsaleno,fprice,fpicname from tfood where styno=? and fstate=1";
this.openConnection();
PreparedStatement ps = connection.prepareStatement(sql); //用连接的 准备statement
ps.setInt(1, a);
ResultSet rs = ps.executeQuery(); // 用准备的statement 执行罗列
fods= new ArrayList<Fod>(6); //初始化集合
ImgConfigUtil picutil =ImgConfigUtil.newInstance();
while(rs.next()){
Fod fd = new Fod();
fd.setFname(rs.getString("fname"));
fd.setFno(rs.getInt("fno"));
fd.setFprice(rs.getDouble("fprice"));
fd.setFsaleno(rs.getInt("fsaleno"));
fd.setFpicname(picutil.getAccesspath()+rs.getString("fpicname"));
fods.add(fd);
}
return fods;
}