总结Jsp开发网站.docx
《总结Jsp开发网站.docx》由会员分享,可在线阅读,更多相关《总结Jsp开发网站.docx(23页珍藏版)》请在冰豆网上搜索。
![总结Jsp开发网站.docx](https://file1.bdocx.com/fileroot1/2023-2/4/fb6cb08b-4026-454c-94a3-0881b923d649/fb6cb08b-4026-454c-94a3-0881b923d6491.gif)
总结Jsp开发网站
总结Jsp开发网站
总分两大方面:
1-代码类
2-显示页面类
代码类:
创建包名一般命名规范:
org+项目名字+包名。
分4个包,分别是dao包(接口代码类名:
类名+dao),impl包(实现接口代码类名+impl),entity包(类文件代码),uitle包(数据库连接类名:
ConnectionManager)。
事例:
entity包(数据库类文件)
导包:
无须导包。
代码:
private类型+XXX(数据库列名);
按Alt键+Shift键+S键进行封装。
dao包(定义接口)
导包:
importjava.util.List;
importorg.text.entity.*;
代码:
/**
*增加板块
*/
publicintadd+类名(类名n)throwsException;
/**
*删除板块
*/
publicintdelete+类名(类名n)throwsException;
[publicintdelete+类名(intn)throwsException;]
/**
*更新板块
*/
publicintupdate+类名(类名n)throwsException;
/**
*查询板块
*/
publicListfind+类名()throwsException;
/**
*带参查询板块
*/
public类名+show+类名(intid)throwsException;
uitle包(数据库连接)
导包:
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.PreparedStatement;
importjava.sql.SQLException;
importjava.sql.ResultSet;
代码:
publicclassConnectionManager
{
privatestaticfinalStringDERIVE="com.microsoft.sqlserver.jdbc.SQLServerDriver";
privatestaticfinalStringURL="jdbc:
sqlserver:
//localhost:
1433;DatabaseName=数据库名字";
privatestaticfinalStringUSER="sa";
privatestaticfinalStringPASS="sa";
publicstaticConnectiongetConnection()
{
Connectioncon=null;
try{
Class.forName(DERIVE);
try{
con=DriverManager.getConnection(URL,USER,PASS);
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}catch(ClassNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
returncon;
}
publicstaticvoidcloseAll(ResultSetrs,PreparedStatementps,Connectioncon)
{
if(rs!
=null)
{
try{
rs.close();
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
if(ps!
=null)
{
try{
ps.close();
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
try{
if(con!
=null&&con.isClosed())
{
try{
con.close();
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}
impl包(实现接口代码)
创建实现接口代码时注意Add添加定义接口。
导包:
importjava.sql.Connection;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.util.ArrayList;
importjava.util.List;
importpany.dao.+接口名;
importpany.entity.+类名;
importpany.uitle.ConnectionManager;
代码:
privateConnectionconn=null;
privatePreparedStatementps=null;
privateResultSetrs=null;
publicintdelete+类名(类名n)throwsException
[publicintdelete+类名(intn)throwsException]
{
//TODOAuto-generatedmethodstub删除
intnum=0;
try{
conn=ConnectionManager.getConnection();
Stringsql="deletefromUsersWhereuID=?
";
ps=conn.prepareStatement(sql);
ps.setInt(1,usersID);
num=ps.executeUpdate();
}catch(RuntimeExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}finally
{
ConnectionManager.closeAll(rs,ps,conn);
}
returnnum;
}
publicintadd+类名(类名n)throwsException
{
//TODOAuto-generatedmethodstub增加
intnum=0;
try
{
con=ConnectionManager.getConnection();
Stringsql="insertinto+表名values(?
?
?
)";
ps=con.prepareStatement(sql);
[
ps.setString(1,n.getUserName());
ps.setString(2,n.getPassword());
ps.setInt(3,n.getStatus());
]根据实际情况写
num=ps.executeUpdate();
}
catch(RuntimeExceptione)
{
e.printStackTrace();
}
finally
{
ConnectionManager.closeAll(rs,ps,conn);
}
returnnum;
}
publicintupdate+类名(类名n)throwsException
{
//TODOAuto-generatedmethodstub更新
intnum=0;
try
{
con=ConnectionManager.getConnection();
Stringsql="数据库语句";
ps=con.prepareStatement(sql);
[
ps.setString(1,user.getPassword());
ps.setInt(2,user.getUserID());
]根据实际情况写
num=ps.executeUpdate();
}
catch(RuntimeExceptione)
{
e.printStackTrace();
}
finally
{
ConnectionManager.closeAll(rs,ps,conn);
}
returnnum;
}
publicListfind+类名()throwsException
{
//TODOAuto-generatedmethodstub查询
Newsn=null;
Listlist=newArrayList();
try
{
Stringsql="select*fromNEWS";
conn=ConnectionManager.getConnection();
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next())
[
{
n=newNews();
n.setNewsID(rs.getInt
(1));
n.setTitle(rs.getString
(2));
n.setContent(rs.getString(3));
n.setWriterDate(rs.getString(4));
list.add(n);
}
]根据实际情况写
}
catch(RuntimeExceptione)
{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
finally
{
ConnectionManager.closeAll(rs,ps,conn);
}
returnlist;
}
publicNewsfindNewsByID(intid)throwsException
{
Newsnews=null;
//TODOAuto-generatedmethodstub但参数的查询
try{
conn=ConnectionManager.getConnection();
Stringsql="select*fromNewswherenewsID=?
";
ps=conn.prepareStatement(sql);
ps.setInt(1,id);
rs=ps.executeQuery();
while(rs.next())
[
{
news=newNews();
news.setNewsID(rs.getInt("newsID"));
news.setTitle(rs.getString("title"));
news.setContent(rs.getString("content"));
news.setWriterDate(rs.getString("writerDate"));
}
]根据实际情况写
}catch(RuntimeExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
finally
{
ConnectionManager.closeAll(rs,ps,conn);
}
returnnews;
}
显示页面类:
导入验证码JSP
<%@pagecontentType="image/jpeg"import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"%>
<%!
ColorgetRandColor(intfc,intbc){//给定范围获得随机颜色
Randomrandom=newRandom();
if(fc>255)fc=255;
if(bc>255)bc=255;
intr=fc+random.nextInt(bc-fc);
intg=fc+random.nextInt(bc-fc);
intb=fc+random.nextInt(bc-fc);
returnnewColor(r,g,b);
}
%>
<%
//设置页面不缓存
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires",0);
//在内存中创建图象
intwidth=60,height=20;
BufferedImageimage=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
//获取图形上下文
Graphicsg=image.getGraphics();
//生成随机类
Randomrandom=newRandom();
//设定背景色
g.setColor(getRandColor(200,250));
g.fillRect(0,0,width,height);
//设定字体
g.setFont(newFont("TimesNewRoman",Font.PLAIN,18));
//画边框
//g.setColor(newColor());
//g.drawRect(0,0,width-1,height-1);
//随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
g.setColor(getRandColor(160,200));
for(inti=0;i<155;i++)
{
intx=random.nextInt(width);
inty=random.nextInt(height);
intxl=random.nextInt(12);
intyl=random.nextInt(12);
g.drawLine(x,y,x+xl,y+yl);
}
//取随机产生的认证码(4位数字)
//Stringrand=request.getParameter("rand");
//rand=rand.substring(0,rand.indexOf("."));
StringsRand="";
for(inti=0;i<4;i++){
Stringrand=String.valueOf(random.nextInt(10));
sRand+=rand;
//将认证码显示到图象中
g.setColor(newColor(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));//调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
g.drawString(rand,13*i+6,16);
}
//将认证码存入SESSION
session.setAttribute("rand",sRand);
//图象生效
g.dispose();
//输出图象到页面
ImageIO.write(image,"JPEG",response.getOutputStream());
out.clear();
out=pageContext.pushBody();
%>
实现验证码:
<%@pagelanguage="java"import="java.util.*"pageEncoding="GBK"%>
<%@pageimport="pany.dao.*"%>
<%@pageimport="pany.dao.impl.*"%>
<%@pageimport="pany.entity.*"%>
<%
request.setCharacterEncoding("GBK");
Stringname=request.getParameter("uname");
Stringpass=request.getParameter("upass");
Stringcheck=request.getParameter("read");
Stringrand=(String)session.getAttribute("rand");//验证码取值
UsersDaousers=newUsersDaoImpl();
Usersus=newUsers();
us.setUserName(name);
us.setPassword(pass);
if(!
rand.equals(check))//验证码判断
{
session.setAttribute("error","验证码错误~!
");
request.getRequestDispatcher("error.jsp").forward(request,response);
}else
{
if(users.selectUser(us)>0)
{
session.setAttribute("LONG",us);
Listlist=newArrayList();
if(application.getAttribute("LONG")!
=null)
{
list=(List)application.getAttribute("LONG");
}
list.add(us);
application.setAttribute("LONG",list);
response.sendRedirect("Talking.jsp");
}
else
{
session.setAttribute("check","输入信息错误~!
");
request.getRequestDispatcher("error.jsp").forward(request,response);
}
}
%>
四大(增删改查)Jsp页面
显示查询页面
导包
<%@pageimport="pany.dao.*"%>
<%@pageimport="pany.entity.*"%>
<%@pageimport="pany.impl.*"%>
[
<%
TextDaono=newTextDaoImpl();
Listlist=no.findUsers();
for(inti=0;i{
Usersnews=(Users)list.get(i);
%>
<%=news.getUsername()%> |
(带Id的显示
给一个值一般是ID名字小写=<%=news.get+ID名字()%>"><%=news.getUsername()%> | )
<%=news.getPassword()%> | ]根据实际情况修改
<%
}
%>
显示修改页面
显示增加页面
导包
<%@pageimport="pany.dao.*"%>
<%@pageimport="pany.entity.*"%>
<%@pageimport="pany.impl.*"%>
代码
<%
request.setCharacterEncoding("gbk");
UsersDaomanDao=newUsersDaoImpl();
Usersman=newUsers();
StringuserName=request.getParameter("Name");
Stringuserpass=request.getParameter("Pass");//得到修改的值
man.setUName(userName);
man.setUPass(userpass);
if(userpass!
=null)
{
intcount=manDao.addUsers(man);
if(count>0){
out.print("alert('成功添加一名管理员。
');location.replace('rights.jsp');");
}else{
out.print("alert('添加失败。
');history.go(-1);");
}