huiyuan.setId(new Date().getTime() + new Double(Math.floor(Math.random() * 1000)).longValue());
时间: 2024-05-20 21:12:41 浏览: 24
这段代码的作用是给一个会员对象设置一个唯一的ID,ID由当前时间戳和一个随机数生成。具体解释如下:
1. `new Date().getTime()`:获取当前时间戳,即从1970年1月1日00:00:00 UTC到当前时间的毫秒数。
2. `Math.floor(Math.random() * 1000)`:生成一个0到999之间的随机整数。
3. `new Double()`:将生成的随机整数转换为Double类型。
4. `longValue()`:将Double类型的随机数转换为长整型。
5. 将当前时间戳和随机数相加作为新的ID,调用`setId()`方法设置给会员对象。
这样生成的ID可以保证在一定程度上的唯一性,但是并不能完全避免重复。
相关问题
private void huiyuanLogin(HttpServletRequest request, HttpServletResponse response) { String accountname = request.getParameter("accountname"); String password = request.getParameter("password"); String filter = MessageFormat.format( "where accountname=''{0}'' and password=''{1}''", accountname, password); Huiyuan huiyuan = (Huiyuan) DALBase.load("huiyuan", filter); String errorurl=request.getParameter("errorurl"); String forwardurl=request.getParameter("forwardurl"); if (huiyuan != null && huiyuan.getPassword().equals(password)) { try { huiyuan.setLogtimes(huiyuan.getLogtimes() + 1); DALBase.update(huiyuan); request.getSession().setAttribute("huiyuan", huiyuan); if (forwardurl != "") response.sendRedirect(SystemParam.getSiteRoot() + forwardurl); else { response.sendRedirect(SystemParam.getSiteRoot() + "/e/huiyuan/accounintfo.jsp"); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { dispatchParams(request, response); request.setAttribute("errormsg", "<label class='error'>系统账户和密码不匹配<label>"); try { request.getRequestDispatcher(errorurl).forward(request, response); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }解释每一行代码
这段代码是一个 Java 代码块,实现的是会员登录功能。下面是每一行代码的解释:
1. `private void huiyuanLogin(HttpServletRequest request, HttpServletResponse response)`:定义了一个名为 `huiyuanLogin` 的方法,其中 `HttpServletRequest` 和 `HttpServletResponse` 分别表示 HTTP 请求和响应。
2. `String accountname = request.getParameter("accountname");`:从 HTTP 请求中获取名为 `accountname` 的参数,并将其赋值给 `accountname` 变量。
3. `String password = request.getParameter("password");`:从 HTTP 请求中获取名为 `password` 的参数,并将其赋值给 `password` 变量。
4. `String filter = MessageFormat.format("where accountname=''{0}'' and password=''{1}''", accountname, password);`:使用 `MessageFormat` 类将 `accountname` 和 `password` 拼接成一个 SQL 查询语句,并将其赋值给 `filter` 变量。
5. `Huiyuan huiyuan = (Huiyuan) DALBase.load("huiyuan", filter);`:使用 `DALBase` 类从数据库中加载会员信息,其中 `huiyuan` 表示表名,`filter` 表示查询条件,将查询结果转换为 `Huiyuan` 类型,并将其赋值给 `huiyuan` 变量。
6. `String errorurl=request.getParameter("errorurl");`:从 HTTP 请求中获取名为 `errorurl` 的参数,并将其赋值给 `errorurl` 变量。
7. `String forwardurl=request.getParameter("forwardurl");`:从 HTTP 请求中获取名为 `forwardurl` 的参数,并将其赋值给 `forwardurl` 变量。
8. `if (huiyuan != null && huiyuan.getPassword().equals(password)) {`:如果 `huiyuan` 不为 null,且 `huiyuan` 的密码与输入的密码相同,则执行下面的代码块。
9. `huiyuan.setLogtimes(huiyuan.getLogtimes() + 1);`:将 `huiyuan` 的登录次数加 1。
10. `DALBase.update(huiyuan);`:使用 `DALBase` 类将更新后的 `huiyuan` 对象保存回数据库中。
11. `request.getSession().setAttribute("huiyuan", huiyuan);`:将 `huiyuan` 对象存储到 HTTP 会话中,以便后续使用。
12. `if (forwardurl != "") response.sendRedirect(SystemParam.getSiteRoot() + forwardurl);`:如果 `forwardurl` 不为空,则将用户重定向到 `forwardurl` 指定的页面。
13. `else response.sendRedirect(SystemParam.getSiteRoot() + "/e/huiyuan/accounintfo.jsp");`:否则将用户重定向到默认页面 `/e/huiyuan/accounintfo.jsp`。
14. `} else {`:如果 `huiyuan` 为 null,或者输入的密码与 `huiyuan` 的密码不相同,则执行下面的代码块。
15. `dispatchParams(request, response);`:将 HTTP 请求转发给其他页面进行处理。
16. `request.setAttribute("errormsg", "<label class='error'>系统账户和密码不匹配<label>");`:设置一个名为 `errormsg` 的请求属性,并将其值设置为错误信息。
17. `request.getRequestDispatcher(errorurl).forward(request, response);`:将 HTTP 请求转发到 `errorurl` 指定的页面,并将响应返回给客户端。
public void save() { String title = request.getParameter("title"); String pubren = request.getParameter("pubren"); String pubtime = request.getParameter("pubtime"); String laiyuan=request.getParameter("laiyuan"); String dcontent = request.getParameter("dcontent"); String tupian2 = request.getParameter("tupian2"); String lanmuid = request.getParameter("lanmuid"); String lanmuming = request.getParameter("lanmuming"); String tuijian=request.getParameter("tuijian"); String hot=request.getParameter("hot"); String zuixin=request.getParameter("zuixin"); String zhaiyao=request.getParameter("zhaiyao"); String style=request.getParameter("style"); SimpleDateFormat sdfxinxi = new SimpleDateFormat("yyyy-MM-dd"); Xinxi xinxi = new Xinxi(); xinxi.setTitle(title == null ? "" : title); xinxi.setPubren(pubren == null ? "" : pubren); xinxi.setPubtime(new Date()); xinxi.setHot(hot!=null?1:0); xinxi.setTuijian(tuijian!=null?1:0); xinxi.setZuixin(zuixin!=null?1:0); xinxi.setClickcount(0); xinxi.setZhaiyao(zhaiyao==null?"":zhaiyao); xinxi.setDcontent(dcontent == null ? "" : dcontent); xinxi.setTupian2(tupian2 == null ? "" : tupian2); xinxi.setAgainstcount(0); xinxi.setAgreecount(0); xinxi.setLanmuid(new Integer(lanmuid)); xinxi.setLanmuming(lanmuming == null ? "" : lanmuming); DALBase.save(xinxi); try { if(style!=null&&style.equals("admin")) response.sendRedirect("xinximanager.do?actiontype=get&seedid=201&lanmuid="+xinxi.getLanmuid()); if(style!=null&&style.equals("huiyuan")) response.sendRedirect("../e/myxinximanager.jsp?seedid=m2"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /****************************************************** *********************** 内部附件支持********************* ***public void attachements(HttpServletRequest request, HttpServletResponse response, String belongid) { DALBase.delete("attachement", MessageFormat.format( " where belongid=''{0}'' and belongtable=''xinxi'' ", belongid)); String[] photos = request.getParameterValues("fileuploaded"); if (photos == null) return; for (int i = 0; i < photos.length; i++) { Attachement a = new Attachement(); a.setType("images"); a.setPubtime(new Date()); a.setBelongfileldname("id"); a.setFilename(photos[i]); a.setBelongid(belongid); a.setBelongtable("xinxi"); a.setUrl(SystemParam.getSiteRoot() + "/upload/temp/" + a.getFilename()); a.setTitle(a.getFilename()); DALBase.save(a); } }解释每一行代码
这是一段Java代码,根据传入的HTTP请求参数保存一个信息(Xinxi)到数据库中。下面是每行代码的解释:
```java
public void save() { // 定义一个公有方法save()
// 从请求参数中获取信息标题、发布者、发布时间、来源、正文内容、图片、栏目ID、栏目名称、推荐、热点和最新等信息
String title = request.getParameter("title");
String pubren = request.getParameter("pubren");
String pubtime = request.getParameter("pubtime");
String laiyuan=request.getParameter("laiyuan");
String dcontent = request.getParameter("dcontent");
String tupian2 = request.getParameter("tupian2");
String lanmuid = request.getParameter("lanmuid");
String lanmuming = request.getParameter("lanmuming");
String tuijian=request.getParameter("tuijian");
String hot=request.getParameter("hot");
String zuixin=request.getParameter("zuixin");
String zhaiyao=request.getParameter("zhaiyao");
String style=request.getParameter("style");
// 定义一个日期格式化对象,格式为"yyyy-MM-dd"
SimpleDateFormat sdfxinxi = new SimpleDateFormat("yyyy-MM-dd");
// 创建一个信息对象
Xinxi xinxi = new Xinxi();
// 将获取到的信息设置到信息对象中
xinxi.setTitle(title == null ? "" : title);
xinxi.setPubren(pubren == null ? "" : pubren);
xinxi.setPubtime(new Date()); // 设置发布时间为当前时间
xinxi.setHot(hot!=null?1:0); // 如果参数hot不为空,则将热点属性设为1,否则设为0
xinxi.setTuijian(tuijian!=null?1:0); // 如果参数tuijian不为空,则将推荐属性设为1,否则设为0
xinxi.setZuixin(zuixin!=null?1:0); // 如果参数zuixin不为空,则将最新属性设为1,否则设为0
xinxi.setClickcount(0); // 设置点击次数为0
xinxi.setZhaiyao(zhaiyao==null?"":zhaiyao);
xinxi.setDcontent(dcontent == null ? "" : dcontent);
xinxi.setTupian2(tupian2 == null ? "" : tupian2);
xinxi.setAgainstcount(0); // 设置反对次数为0
xinxi.setAgreecount(0); // 设置赞同次数为0
xinxi.setLanmuid(new Integer(lanmuid)); // 将栏目ID设置到信息对象中
xinxi.setLanmuming(lanmuming == null ? "" : lanmuming);
// 调用数据访问层的save()方法保存信息对象到数据库中
DALBase.save(xinxi);
try {
// 根据请求参数中的style值进行不同的重定向操作
if(style!=null&&style.equals("admin"))
response.sendRedirect("xinximanager.do?actiontype=get&seedid=201&lanmuid="+xinxi.getLanmuid());
if(style!=null&&style.equals("huiyuan"))
response.sendRedirect("../e/myxinximanager.jsp?seedid=m2");
} catch (IOException e) {
e.printStackTrace();
}
}
// 定义一个公有方法attachements()用于支持内部附件
public void attachements(HttpServletRequest request, HttpServletResponse response, String belongid) {
// 删除已存在的附件
DALBase.delete("attachement", MessageFormat.format(" where belongid=''{0}'' and belongtable=''xinxi'' ", belongid));
// 获取请求参数中的上传的图片文件名
String[] photos = request.getParameterValues("fileuploaded");
if (photos == null)
return;
// 遍历图片文件名数组,创建附件对象并保存到数据库中
for (int i = 0; i < photos.length; i++) {
Attachement a = new Attachement();
a.setType("images");
a.setPubtime(new Date()); // 设置附件发布时间为当前时间
a.setBelongfileldname("id");
a.setFilename(photos[i]);
a.setBelongid(belongid);
a.setBelongtable("xinxi");
a.setUrl(SystemParam.getSiteRoot() + "/upload/temp/" + a.getFilename());
a.setTitle(a.getFilename());
DALBase.save(a);
}
}
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)