JAVA中的FtpClient与FTPClient,并实现jsp页面下载ftp服
务器上的文件
这段时间一直在研究Java如何访问Ftp,搞了一段时间了,也有一定的了解。故此记录一下。
ftp和FTP我个人觉得FTP更符合我们程序员的口味,不管是方法命名还是API的详细与否,或者是开发平台的问题,FTP毕竟是Apache的东西,做的就是不错。
其实web开发中一般都会涉及到编码问题,所以web上传下载一定会有中文乱码的问题存在,而FTP对中文的支持比ftp要好多了。
使用ftpClient不需要导入其它jar包,只要你使用java语言开发就行了,而使用FTPClient 需要使用commons-net-1.4.1.jar和jakarta-oro-2.0.8.jar,当然jar版本随便你自己。
话不多说,上代码!
FTP服务器的文件目录结构图:
一、FtpClient
FtpClient是属于JDK的包下面的类,但是jdkapi并没有对此作介绍,在中文支持上面也有一定的限制。
本段代码中的Ftp服务器的IP地址,用户名和密码均通过SystemConfig.properties文档获取
Ftp_client.java
[java]view plain copy
1.package com.iodn.util;
2.
3.import java.io.ByteArrayOutputStream;
4.import java.io.File;
5.import java.io.FileInputStream;
6.import java.io.FileOutputStream;
7.import java.io.IOException;
8.import java.util.ResourceBundle;
9.import https://www.doczj.com/doc/488256030.html,.TelnetInputStream;
10.import https://www.doczj.com/doc/488256030.html,.TelnetOutputStream;
11.import https://www.doczj.com/doc/488256030.html,.ftp.FtpClient;
12.
13.public class Ftp_client {
14.
15.//FTP客户端
16.private FtpClient ftpClient;
17.private ResourceBundle res=null;
18./**
19. * 连接FTP服务器
20. * @param path 指定远程服务器上的路径
21. */
22.public Ftp_client(String path){
23.
24. res = ResourceBundle.getBundle("com.iodn.util.SystemConfig");//
获取配置文件propeties文档中的数据
25.try{
26. ftpClient=new FtpClient(res.getString("Properties.ftpHostIp"));/
/如果不想使用配置文件即可将数据写死(如:192.168.1.10)
27. ftpClient.login(res.getString("Properties.ftpUser"), res.getStri
ng("Properties.ftpPassword"));//Ftp服务器用户名和密码
28. ftpClient.binary();
29. System.out.println("Login Success!");
30.if(path.length()!=0){
31.//把远程系统上的目录切换到参数path所指定的目录(可不用设置,上传下
载删除时加Ftp中的全路径即可)
32. ftpClient.cd(path);
33. }
34. }catch(Exception e){
35. e.printStackTrace();
36. }
37. }
38.
39./**
40. * 上传文件
41. * @param remoteFile
42. * @param localFile
43. */
44.public boolean upload(String remoteFile, String localFile){
45.boolean bool=false;
46. TelnetOutputStream os=null;
47. FileInputStream is=null;
48.try{
49. os=ftpClient.put(remoteFile);
50. is=new FileInputStream(new File(localFile));
51.byte[] b=new byte[1024];
52.int c;
53.while((c=is.read(b))!=-1){
54. os.write(b, 0, c);
55. }
56. bool=true;
57. }catch(Exception e){
58. e.printStackTrace();
59. System.out.println("上传文件失败!请检查系统FTP设置,并确认FTP
服务启动");
60. }finally{
61.if(is!=null){
62.try {
63. is.close();
64. } catch (IOException e) {
65. e.printStackTrace();
66. }
67. }
68.if(os!=null){
69.try {
70. os.close();
71. } catch (IOException e) {
72. e.printStackTrace();
74. }
75. closeConnect();
76. }
77.return bool;
78. }
79./**
80. * 下载文件
81. * @param remoteFile 远程文件路径(服务器端)
82. * @param localFile 本地文件路径(客户端)
83. */
84.
85.public void download(String remoteFile, String localFile) {
86. TelnetInputStream is=null;
87. FileOutputStream os=null;
88.try{
89.//获取远程机器上的文件filename,借助TelnetInputStream把该文
件传送到本地。
90. is=ftpClient.get(remoteFile);
91. File file=new File(localFile);
92. os=new FileOutputStream(file);
93.byte[] b=new byte[1024];
94.int c;
95.while((c=is.read(b))!=-1){
96. os.write(b,0,c);
97. }
98. }catch(Exception e){
99. e.printStackTrace();
100. System.out.println("下载文件失败!请检查系统FTP设置,并确认FTP服务启动");
101. }finally{
102.if(is!=null){
103.try {
104. is.close();
105. } catch (IOException e) {
106. e.printStackTrace();
107. }
108. }
109.if(os!=null){
110.try {
111. os.close();
112. } catch (IOException e) {
113. e.printStackTrace();
114. }
116. closeConnect();
117.
118. }
119. }
120.
121.// 删除文件至FTP通用方法
122.
123.public void deleteFileFtp(String fileName){
124.try {
125. ftpClient.sendServer("dele " + fileName + "\r\n");
126. } catch (Exception e) {
127. System.out.println("删除文件失败!请检查系统FTP设置,并确认FTP服务启动");
128. }finally{
129. closeConnect();//关闭FTP连接
130. }
131.
132. }
133.
134./**
135. * Ftp下载返回byte[]字节数组供前端下载使用
136. * @param SourceFileName
137. * @return
138. * @throws Exception
139. */
140.public byte[] downFileByte(String SourceFileName) throws Exception {
141.//ftpClient.binary(); //一定要使用二进制模式
142. TelnetInputStream ftpIn = ftpClient.get(SourceFileName); 143. ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); 144.byte[] buf = new byte[204800];
145.int bufsize = 0;
146.while ((bufsize = ftpIn.read(buf, 0, buf.length)) != -1) { 147. byteOut.write(buf, 0, bufsize);
148. }
149.byte[] return_arraybyte = byteOut.toByteArray();
150. byteOut.close();
151. ftpIn.close();
152.return return_arraybyte;
153. }
154./**
155. * 关闭FTP连接
156. */
157.public void closeConnect(){
158.try{
159. ftpClient.closeServer();
160. }catch(Exception e){
161. e.printStackTrace();
162. }
163. }
164.
165.public static void main(String[] args) throws Exception {
166. Ftp_client test=new Ftp_client("/zhou");
167.// test.deleteFileFtp("20140412.zip");
168.// test.upload("20141201.xls", "E:\\201412.xls");//上传文件
169.//test.download("321.txt", "E:\\111.txt");//下载
170. test.downFileByte("321.txt");
171.
172. }
173.
174.
175.
176.}
二、FTPClient
FTPClient是Apache包下面的类,主要依靠commons-net-1.4.1.jar,支持中文上传下载。
本段代码中的Ftp服务器的IP地址,用户名和密码均通过SystemConfig.properties文档获取
FTPClientHelper.java
[java]view plain copy
1.package com.iodn.util;
2.
3.import java.io.ByteArrayOutputStream;
4.import java.io.File;
5.import java.io.FileInputStream;
6.import java.io.FileOutputStream;
7.import java.io.InputStream;
8.import java.io.OutputStream;
9.import java.util.Date;
10.import java.util.ResourceBundle;
11.
12.import https://www.doczj.com/doc/488256030.html,.ftp.FTPClient;
13.import https://www.doczj.com/doc/488256030.html,.ftp.FTPClientConfig;
14.import https://www.doczj.com/doc/488256030.html,.ftp.FTPFile;
15.import https://www.doczj.com/doc/488256030.html,.ftp.FTPReply;
16.
17.public class FTPClientHelper {
18.
19.//FTP客户端
20.private FTPClient ftpClient;
21.private ResourceBundle res=null;
22./**
23. * 连接FTP服务器
24. * @param remotePath //设定当前需要操作的目录
25. */
26.public FTPClientHelper(String remotePath){
27.
28. res = ResourceBundle.getBundle("com.iodn.util.SystemConfig");
29.try{
30. ftpClient=new FTPClient();
31. ftpClient.connect(res.getString("Properties.ftpHostIp"), 21);//F
TP服务器IP地址
32. ftpClient.login(res.getString("Properties.ftpUser"), res.getStri
ng("Properties.ftpPassword"));//FTP服务器用户名和密码
33. ftpClient.setDataTimeout(120000);
34.
35.// 下面三行代码必须要,而且不能改变编码格式,否则不能正确下载中文文
件
36. ftpClient.setControlEncoding("GBK");
37. FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_
UNIX);
38. conf.setServerLanguageCode("zh");
39. System.out.println("Login Success!");
40.int reply=ftpClient.getReplyCode();
41.if(!FTPReply.isPositiveCompletion(reply)){
42. ftpClient.disconnect();
43. ftpClient=null;
44. }
45.//转移到设置的目录下
46.if(remotePath!=null && !remotePath.equals("")){
47. ftpClient.changeWorkingDirectory(remotePath);
48. System.out.println("file success");
49. }
50.
51. }catch(Exception e){
52. e.printStackTrace();
53. }
54. }
55.
56./**
57. * 上传文件
58. * @param remoteFile
59. * @param localFile
60. */
61.public boolean upload( String localFile){
62.boolean bool=false;
63.if(ftpClient!=null){
64.try{
65. File file=new File(localFile);
66. String remoteFile=file.getName();
67. System.out.println(remoteFile);
68. InputStream is=new FileInputStream(file);
69. ftpClient.storeFile(remoteFile, is);
70. is.close();
71. ftpClient.logout();
72. bool=true;
73. }catch(Exception e){
74. e.printStackTrace();
75. System.out.println("上传文件失败!请检查系统FTP设置,并确认
FTP服务启动");
76. }finally{
77. closeConnect();
78. }
79. }
80.
81.return bool;
82. }
83./**
84. * 下载文件
85. * @param remoteFile 远程文件路径(服务器端)
86. * @param fileName 需要下载的文件名
87. * @param localFile 本地文件路径(客户端)
88. */
89.
90.public boolean download(String fileName, String localPath) {
91.boolean bool=false;
92.if(ftpClient!=null){
93.try{
94.//ftpClient.changeWorkingDirectory(remoteFile);//转移到
FTP服务器目录
95. FTPFile[] files= ftpClient.listFiles();
96.for(FTPFile file:files){
97.if(file.getName().equals(fileName)){
98. System.out.println("---start---"+System.curren
tTimeMillis());
99. String path=localPath+"\\"+file.getName(); 100. System.out.println(path);
101. File localFile=new File(path);
102. OutputStream ops=new FileOutputStream(localFi le);
103. ftpClient.retrieveFile(file.getName(), ops);
104. ops.close();
105. System.out.println("---ender---"+System.curre ntTimeMillis());
106. bool=true;
107.break;
108. }
109. }
110. }catch(Exception e){
111. e.printStackTrace();
112. System.out.println("下载文件失败!请检查系统FTP设置,并确认FTP服务启动");
113. }finally{
114. closeConnect();
115. }
116. }
117.return bool;
118. }
119.
120.// 删除文件至FTP通用方法
121.
122.public boolean deleteFileFtp(String fileName){
123.boolean bool=false;
124.if(ftpClient!=null){
125.try {
126. ftpClient.deleteFile(fileName);
127. bool=true;
128. } catch (Exception e) {
129. System.out.println("删除文件失败!请确定文件是否存在");
130. }finally{
131. closeConnect();//关闭FTP连接
132. }
133. }
134.return bool;
135. }
136.
137./**
138. * 下载文件
139. * 返回byte[]
140. * @param fileName 需要下载的文件名
141. * @return
142. * @throws Exception
143. */
144.public byte[] downFileByte(String fileName){
145.byte[] return_arraybyte=null;
146.if(ftpClient!=null){
147.try{
148. FTPFile[] files= ftpClient.listFiles();
149.for(FTPFile file:files){
150.if(file.getName().equals(fileName)){
151. InputStream ins=ftpClient.retrieveFileStream(file.getName() );
152. ByteArrayOutputStream byteOut = new ByteArrayOutputStream() ;
153.byte[] buf = new byte[204800];
154.int bufsize = 0;
155.while ((bufsize = ins.read(buf, 0, buf.length)) != -1) { 156. byteOut.write(buf, 0, bufsize);
157. }
158. return_arraybyte = byteOut.toByteArray();
159. byteOut.close();
160. ins.close();
161.break;
162. }
163. }
164. }catch(Exception e){
165. e.printStackTrace();
166. }finally{
167. closeConnect();
168. }
169. }
170.return return_arraybyte;
171. }
172./**
173. * 关闭FTP连接
174. */
175.public void closeConnect(){
176.try{
177. ftpClient.disconnect();
178. }catch(Exception e){
179. e.printStackTrace();
180. }
181. }
182.
183.public static void main(String[] args) {
184. FTPClientHelper test=new FTPClientHelper("/zhou");
185.boolean bool=false;
186.//bool= test.deleteFileFtp("光路由.doc");
187.//bool=test.upload("你好.xls", "E:\\你好.xls");//上传文件
188.//bool=test.download("你好.xls", "D:\\");//下载
189.//bool=test.downloadToInputStream("/zhou/123.txt");
190. System.out.println(""+bool);
191. }
192.
193.
194.
195.}
三、对web的支持,如何通过jsp页面直接下载
对web的支持,必须要用到serlvet,调用java方法去获取FTP服务器上的文件流。
1、HelpServlet
[java]view plain copy
1.package com.iodn.servlets;
2.
3.import java.io.BufferedOutputStream;
4.import java.io.IOException;
5.import java.io.OutputStream;
6.
7.import javax.servlet.ServletException;
8.import javax.servlet.http.HttpServlet;
9.import javax.servlet.http.HttpServletRequest;
10.import javax.servlet.http.HttpServletResponse;
11.
12.import com.iodn.util.FTPClientHelper;
13.
14./**
15. * Servlet implementation class HelpServlet
16. */
17.public class HelpServlet extends HttpServlet {
18.private static final long serialVersionUID = 1L;
19.
20./**
21. * @see HttpServlet#HttpServlet()
22. */
23.public HelpServlet() {
24.super();
25.// TODO Auto-generated constructor stub
26. }
27.
28./**
29. * @see HttpServlet#doGet(HttpServletRequest request, HttpServletRespons
e response)
30. */
31.protected void doGet(HttpServletRequest request, HttpServletResponse res
ponse) throws ServletException, IOException {
32. request.setCharacterEncoding("UTF-8");
33.this.downFtpFile("/HelpDocument", "NJPTPXHelper.chm", request, respo
nse);
34. }
35.
36./**
37. * @see HttpServlet#doPost(HttpServletRequest request, HttpServletRespon
se response)
38. */
39.protected void doPost(HttpServletRequest request, HttpServletResponse re
sponse) throws ServletException, IOException {
40.// TODO Auto-generated method stub
41. }
42.
43.
44./**
45. * 根据路径下载Ftp中的文件
46. * @param remoteFile
47. * @param fileName
48. * @param request
49. * @param response
50. * @throws IOException
51. */
52.public void downFtpFile(String remoteFile,String fileName,HttpServletReq
uest request,HttpServletResponse response) throws IOException{
53.// 以流的形式下载文件。
54.//Ftp_client test=new Ftp_client();
55. FTPClientHelper ftpClient=new FTPClientHelper(remoteFile);
56.//test.closeConnect();//关闭连接
57.byte[] buffer=null;
58.try {
59. buffer = ftpClient.downFileByte(fileName);//根据文件名下载FTP服务
器上的文件
60. } catch (Exception e) {
61.// TODO Auto-generated catch block
62. e.printStackTrace();
63. }
64.// 清空response
65. response.reset();
66. response.setContentType("text/html;charset=UTF-8");
67.// 设置response的Header
68.// response.addHeader("Content-Disposition", "attachment;file
name=" + new String(filename.getBytes("UTF-8"),"ISO-8859-1"));
69. response.addHeader("Content-Disposition", "attachment;filename=" + n
ew String(fileName.getBytes(),"ISO-8859-1"));
70.//response.addHeader("Content-Length", "" + fis.length());
71. OutputStream toClient = new BufferedOutputStream(response.getOutputS
tream());
72. response.setContentType("application/octet-stream");
73. toClient.write(buffer);
74. toClient.flush();
75. toClient.close();
76.
77. }
78.}
1.<%@ page language="java" contentType="text/html; charset=UTF-8"
2. pageEncoding="UTF-8"%>
3.<%@ taglib uri="https://www.doczj.com/doc/488256030.html,/jsp/jstl/core" prefix="c" %>
4.<%
5.String path = request.getContextPath();
6.String basePath = request.getScheme()+"://"+request.getServerName()+":"+requ
est.getServerPort()+path+"/";
7.%>
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18. HelpServlet
" target="_self">
19.
20.
网站帮助
21.
22.
23.
结果:
如何在JSP中使用MYSQL中的登录界面 1.和创建登录的JSP界面,action指向servlet中的方法 2.创建一个新的字符用于存储新的数字和字符String msg=""; 3.获取JSP中输入的数据。String account = request.getParameter("name"); 4.Try语句调用后面的方法。先写方法 方法: 连接数据库和MYSQL中方法一样 try{ String driverName="com.mysql.jdbc.Driver"; //数据库用户名 String userName="root"; //密码 String userPasswd="123456"; //数据库名 String dbName="xueshenbiao"; //表名 String tableName="nima"; //联结字符串 String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+u serPasswd; Class.forName("com.mysql.jdbc.Driver").newInstance(); java.sql.Connection connection=DriverManager.getConnection(url); Statement statement = connection.createStatement(); 重点如下: 之前习惯了用select去巡查SQL中数据,现在因为是验证登录所以只要比对是否有一样的数据即可,现在采用SELECT count(*)用于计数,如果返回是0说明不存在登录的用户,返回1说明存在,返回>1说明不止一个。 5.接下来寻找个数用while(rs.next())寻找个数 6.和删除一样用result返回参数. 7.此时的result有了自己的判断力用于前面的判断。 跳回前面的try语句 用If()else语句来判断登录的界面
jsp页面验证码源代码 在java后台中生成验证码的ImageIO传到前端页面显示,同时把验证码的v alue值传入session 中用于与用户输入的验证码进行匹配,在用户验证中使用ajax技术,在不刷新页面的同时进行验证码验证。 程序结构图: VerifyCodeUtils程序主要内容为通过Java生成验证码的图片,以及验证码的value值,程序如下: package utils; import java.awt.Color; import java.awt.Font;
import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.util.HashMap; import java.util.Map; import java.util.Random; publicclass VerifyCodeUtils { privatestatic BufferedImage image = null; privatestatic Random random = new Random(); //在自己定义的一些数中,生成4位随机数 publicstatic String getVerifyCode() { String str = ""; char[] code = newchar[]{'A','B','C','D','E','F','G','H','I ','J','K','L','M','N','P','Q','R','S','T','U', 'V','W','X','Y','Z','a','b','c','d','e','f','g','h','i',' j','k','m','n','p','q','r','s','t', 'u','v','w','x','y','z','2','3','4','5','6','7','8','9'}; Random random = new Random(); for(int i = 0; i <4; i++) { str += String.valueOf(code[random.nextInt(code.length)]); } return str; }
静态的登录界面的设计login.htm,代码如下: Html代码 1. 2.
3.系统登录 21. | 22.
使用JSP做了一个简单的登录框架2008-05-05 11:47 功能分析:当用户提交表单时,在当前的页面判断用户的名称及密码是否正确,如果不正确则提示“登录失败”。如果正确则跳转到“欢迎界面”。 问题:防止用户未经登录而直接访问“欢迎界面”。 解决方法:在登录界面里设置session的属性值,在欢迎界面里判断session 的属性值不等于空。此时可以判断出用户是否是通过登录界面登录成功之后跳转到欢迎界面的! 共两个文档 login.jsp ; 登录界面,提供一个表单供用户输入,并判断是否正确 welcome.jsp : 欢迎界面,登录成功后显示信息,如果用户未经登录直接访问,则提示未登录,并自动跳转到登录界面! login.jsp 代码如下: <%@ page contentType="text/html;charset=gb2312"%>
<% //判断表单是否输入了内容if(request.getParameter("uname")!=null && request.getParameter("upass")!=null){ //获取表单的内容 String name = request.getParameter("uname"); String password = request.getParameter("upass"); //判断输入的内容是否正确,此处使用字符串与变量进行比较,可以防止第一次运行时出现空指针错误 if("yk".equals(name)&&"123".equals(password)) { //设置Session的属性值,用于wellcome页面进行判断是否有属性 session.setAttribute("flag","ok"); //跳转到欢迎页面 response.sendRedirect("welcome.jsp"); }else { %>
课程设计报告 课程名称 Web开发技术 系别:工程技术系 专业班级:计科0801 学号: 0811080105 姓名:方海入 课程题目:使用JSP实现网站用户注册和登录 完成日期: 2011年6月 指导老师:林宝平 2011年 6月 24日
课程设计目的1.培养综合运用所学知识解决实际问题的能力;2.培养独立查找资料进行自学的能力; 3.熟悉网站常用基本模块的实现方法。 课程设计要求1.使用HTML表单提交用户信息;2.使用JavaScript验证用户输入信息;3.使用AJAX技术检测用户名是否可用;4.使用JDBC连接数据库。 课程设计注意事项1.可以相互讨论,但应独立完成; 2.代码应当整洁规范,清晰易读; 3.界面应当简洁漂亮,各种提示清晰完整。 课程设计内容 设计一个公司用户登录和注册的网站,并使用JSP技术连接SQL数据库,实现网页的用户登录注册功能
课程设计简要操作步骤1.使用HTML语言编写设计网站登录页面代码。 2.使用SQL SERVER创建用户账户密码数据库。 3.使用ODBC连接数据库和登录界面。 4.连接输入数据测试是否正确。 课程设计心得体会通过此次课程实训,是我更加明白理论与实践相结合的重要性,光学习理论是远远不够的,还要能在实际的操作上熟练的运用才是最关键的,只有不断的提高自我的实际操作水平,才能学以致用,为以后的工作打下坚实的基础。 课 程设计评语及成绩评 语 成 绩 指导教师 (签名) 2011年6月日
附件: 摘要 本文的叙述围绕着如何实现一个功能完善的网站进行展开。依据本次毕业设计的要求,主要从可行性分析、需求分析、总体设计、详细设计、编码、测试几个阶段进行毕业设计论文的编写。 接着在需求分析中,根据用户的角度,分析了所设计的网站需要实现哪些基本功能;从设计者的角度,分析了设计一个网站所用的编辑工具和编程语言及网站的软硬件开发环境。根据分析的结果,主要采用FrontPage,Dreamweaver编辑器,SQL 2000结合https://www.doczj.com/doc/488256030.html,(vbscript,j avascript)编程编程语言,进行网站的页面设计;网站的主要功能主要分为新闻、下载、友情链接、网上调查,图片几大模块。 详细设计阶段的任务就是把解法具体化。在详细设计阶段,依据总体分析的结果,进行具体页面的组织和数据库结构的设计。页面的组织主要是设计网页之间的联系,同时根据这些联系实现网页基本功能的构架;数据库的设计主要考虑各程序模块的基本功能,设计相应的表格、字段,用来存贮相应的数据记录,为页面的生成提供数据来源。 在编码阶段,进行具体网页的编码设计。在设计中,根据网页的程序界面,表单,需要的数据等,写出实际的程序代码。由于本次设计的页面比较多,不可能尽数地写出全部程序的代码,因此,在这一章节中,主要解释了在此次设计中主要用到的页面设计和数据库的编码技术。 测试的主要任务是为了发现程序中的错误,软件测试的过程亦是程序运行的过程。在本次设计中,主要采用边测试边修改的方法,在测试网页的同时根据结果及时进行相应的修改。在测试过程中,主要从外观、链接、速度方面测试网页是否能够实现相应的功能。 关键字JSP、WEB、Dreamweaver、SQL 2000 ABSTRACT The textual description is around how to realizes the perfect website of a function pr oceeds to launch.According to the request of this graduate design, primarily analyze from the viability assessment, analysis of need, total design, detailed design, code design, test this five stage to proceed this graduate design. Immediately after analyze the inside in the need, according to the customer's angle, analy zed basic functions of which demand in website for designing of realizeses;From angle of the design, analyzed to design a development environment of soft hardware for editor tool for us ing with weaving the language and website.According to the analysis's result, mostly use Drea mweaver , and connected JSP( vbscript, javascript) language, proceed the design of website's p age;The website's main function is primarily divided with the news, forums, message the guest book , net investigate, amusement. The mission of the detailed design stage is to embody the solution method.In the result o f the total analysis of detailed design stage, basis, proceed the buildup of the concrete page is with the design of construction database.Page for contact for of buildup primarily is°which design web paging, at the same time according to these contacts realizing basic function of web page;Main each procedure mold of consideration of the database's design piece form, wor
servlet+JSP实现用户登录功能 一、Servlet+JSP+JavaBean开发模式(MVC)介绍 Servlet+JSP+JavaBean模式(MVC)适合开发复杂的web应用,在这种模式下,servlet负责处理用户请求,jsp负责数据显示,javabean负责封装数据。Servlet+JSP+JavaBean模式程序各个模块之间层次清晰,web开发推荐采用此种模式。 这里以一个最常用的用户登录注册程序来讲解Servlet+JSP+JavaBean开发模式,通过这个用户登录注册程序综合案例,把之前的学过的XML、Xpath、Servlet、jsp的知识点都串联起来。 二、创建MVC架构的Web项目 在MyEclipse中新创建一个webmvcframework项目,导入项目所需要的开发包(jar包),创建项目所需要的包,在java 开发中,架构的层次是以包的形式体现出来的
一个良好的JavaWeb项目架构应该具有以上的11个包,这样显得层次分明,各个层之间的职责也很清晰明了,搭建JavaWeb项目架构时,就按照上面的1~11的序号顺序创建包:domain→dao→dao.impl→service→service.impl→web.controller→web.UI→web.filter→web.listener→util→junit.test,包的层次创建好了,项目的架构也就定下来了,当然,在实际的项目开发中,也不一定是完完全全按照上面说的来创建包的层次结构,而是根据项目的实际情况,可能还需要创建其他的包,这个得根据项目的需要来定了 在src目录(类目录)下面,创建用于保存用户数据的xml文件(DB.xml) 在WEB-INF目录下创建一个pages目录,pages目录存放系统的一些受保护(不允许用户直接通过URL地址访问)的jsp 页面,用户要想访问这些受保护的jsp页面,那么只能通过me.gacl.web.UI这个包里面的Servlet 创建好的项目如下图(图-1)所示: 图-1
https://www.doczj.com/doc/488256030.html,/blog/20091213/77.html 按以下要求建立用户帐号信息表: 用户ID:字符型,最大长度20 用户姓名:字符型,最大长度20 用户密码:字符型,最大长度20 编写用户登陆页面。当用户输入正确的用户ID及密码后迁移到“登录成功”提示页面。否则用户重新进行登录。 11111 张三111111 22222 李四222222 33333 张五333333 编写这个project时主要遇到了一个jsp页面的url传值问题,其次就是一些逻辑处理的问题 url传参的String n = request.getParameter(“wrongid”); if(n == “wrongid1″)判断不了。请教了某个高手后才知道java的字符串是不能这样判断的,要用 n.equals()方法。 java中字符串的存储方法不一样,不是基本数据类型。 至此三个实验全部完成了,发现java还是挺有意思的,我很想学java啊……..可惜最近忙的要死,都抽不出时间看书。 我这人最大的杯具就是对什么都感兴趣,什么都懂一点,但是什么都不精通…….. 附上核心代码和工程压缩包下载: index.jsp
login.jsp String userid = request.getParameter("username"); String password = request.getParameter("password");本例需要的软件和运行环境: 1、Windows2000 Server操作系统 2、jdk1.4 3、JCreator2.5(java源码编辑调试器,吐血推荐!) 4、Macromedia JRun MX 5、Macromedia Dreamweaver MX(非必需) 6、MySQL数据库(最好安装MySQL Control Center) 一、数据库设计 用MySQL Control Center打开MySQL数据库,新建数据库shopping,在其下新建表tbl_user,其中各字段设置如下: 二、编写连接数据库bean:DBConn.java //DBConn.java //include required classes import java.sql.*; //========================================== // Define Class DBConn //========================================== public class DBConn { public String sql_driver = "org.gjt.mm.mysql.Driver"; public String sql_url = "jdbc:mysql://localhost:3306"; public String sql_DBName = "shopping"; public String user = "sa"; public String pwd = "";
Connection conn = null; Statement stmt = null; ResultSet rs = null; public boolean setDriver(String drv) { this.sql_driver = drv; return true; } public String getDriver() { return this.sql_driver; } public boolean setUrl(String url) { this.sql_url = url; return true; } public boolean setDBName(String dbname) { this.sql_DBName = dbname; return true; } public String getDBName() { return this.sql_DBName; } public boolean setUser(String user) { https://www.doczj.com/doc/488256030.html,er = user;
JavaWeb项目实现登录注册 一. 电脑,eclipse,tomcat,mysql数据库 数据库连接架包 二.项目开始 1.打开eclipse创建一个新的JavaWeb项目,将下载的数据库连接架包放到WebContent 目录下的WEB-INFO中的lib下,右键加入构建路劲。 2.在WebContent下新建一个login.jsp的登录页面 代码如下: <%@page language="java"contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String path = request.getContextPath();//获取项目名String basePath = request.getScheme() + "://" + request.getServerName() + ":"+ request.getServerPort() + path + "/"; //项目的绝对路径
%>
目录 1 前言............................................................................................................................页码 1.1 问题提出 ................................................................................................................页码 1.2 研究意义 ................................................................................................................页码 2 系统需求分析..............................................................................................................页码 2.1 系统功能分析 ........................................................................................................页码 2.2 系统功能模块设计 ................................................................................................页码 3 数据库的概念结构设计...........................................................................................页码 3.1数据抽象 .................................................................................................................页码 3.2设计全局概念模式 .................................................................................................页码 4 数据库的逻辑设计 ....................................................................................................页码 4.1形成初始的关系模式 .............................................................................................页码 4.2关系模式的规范化处理 .........................................................................................页码5系统的实现与调试......................................................................................................页码6 总结 ................................................................................................................................页码参考文献.......................................................................................................................... 页码