随笔-26  评论-13  文章-46  trackbacks-0
1 表单的另一种参数传递方法:
<form name="form1<%=id%>" method="post" action="bookOrder.jsp?bookid=<%=id%>">

<img src="images/all1.gif" width="40" height="40" onClick="submit()"></form>



2 防止为登陆浏览:
if(session.getAttribute("UserName")==null||session.getAttribute("UserName")=="")

{



out.println("<Script language=´javascript´>");

out.println("alert(´对不起!请你登陆后再订阅´)");

out.println("history.go(-1)");

out.println("</script>");

}

else

{



%>



3验证 用户信息的合法性:




<%@ page language="java" import="java.sql.*" %>

<jsp:useBean id="bookM" scope="page" class="date.SearchBookCar" />

<%! String bookid,bookNumS; int Id,bookNumI;

%>



<%



bookid=request.getParameter("bookid");//得到ID

bookNumS=request.getParameter("bookfield");//得到数量

if(bookNumS!=""&bookNumS!=null)

{



try

{

bookNumI=Integer.parseInt(bookNumS);//转换成int类型

Id=Integer.parseInt(bookid) ;



}

catch(NumberFormatException e)

{

out.println("<Script language=´javascript´>");

out.println("alert(´对不起,您输入的数字格式不对!´)");

out.println("history.back(-1)");

out.println("</script>");



}

boolean one=bookM.bookQuery(Id,bookNumI,""+session.getAttribute("loginTime"),""+session.getAttribute("UserName"));

if(one)

{

out.println("<Script language=´javascript´>");

out.println("alert(´提交成功!´)");

out.println("history.back(-1)");

out.println("</script>");



}

else

{

out.println("<Script language=´javascript´>");

out.println("alert(´您已经订过此书!´)");

out.println("history.back(-1)");

out.println("</script>");



}

}

bookM.bclose();

}%>

4表单的检验
<form action="search.jsp" name="search_form" methor="post">

<input type="submit" name="Submit3" value="确定" onclick=search_sub()>

<script language="javascript">

function search_sub()

{

if(document.search_form.keyword.value==""|document.search_form.keyword.value=="请输入关键字")

{

window.alert("请输入关键字!");

document.search_form.keyword.focus();

return;

}

else

{

document.search_form.submit();

}

}

5搜索的sql代码:
String strSQL="Select * from hotbook";

if(way!=null&&keyword!=null)

{



//////////////出版社********************

if(way.trim().equals("3"))

{

strSQL="SELECT * FROM hotbook where";

strSQL=strSQL+" 出版社 like "+"´%"+keyword+"%´";

}

//////////////出版日期********************

if(way.trim().equals("4"))

{

strSQL="SELECT * FROM hotbook where";

strSQL=strSQL+" 出版日期 like ´%"+keyword+"%´";

}

//////////////书名********************

if(way.trim().equals("1"))

{

strSQL="SELECT * FROM hotbook where";

strSQL=strSQL+" 书名 like ´%"+keyword+"%´";

}

//////////////作者********************

if(way.trim().equals("2"))

{

strSQL="SELECT * FROM hotbook where";

strSQL=strSQL+" 作者 like ´%"+keyword+"%´";

}

//////////////ISBN********************

if(way.trim().equals("5"))

{

strSQL="SELECT * FROM hotbook where";

strSQL=strSQL+" ISBN like ´%"+keyword+"%´";

}

}



6分页显示
if (strSQL!=null)

{

Connection con=yy.getConn();

Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);

ResultSet RSa=stmt.executeQuery(strSQL);

RSa.last();

int qikan_num=0;//每页实际页数

int pageNO=1;//第几页

int pageRecorders=5;//每页10条记录

int pageTotal=RSa.getRow();//共几条

int pageCount=pageTotal/pageRecorders;//每页10条共几页

String strPage = request.getParameter("pagenum");//得到传递上来的页数

if(strPage==null)

{

//表明在QueryString中没有page这一个参数,此时显示第一页数据

pageNO = 1;

}

else

{

//将字符串转换成整型

pageNO= java.lang.Integer.parseInt(strPage);

if(pageNO<1)

{pageNO = 1;}

}

if(pageCount>=0)

{

//将记录指针定位到待显示页的第一条记录//的前一条上

if((pageNO-1)==0)//如果为第一页,则指向第0条

{

RSa.beforeFirst();//如果为第一条记录,

}

else RSa.absolute((pageNO-1) * pageRecorders);//如果为其它,则指向当前条的下一条

}



%>



<table width="772" border="0">

<tr>

<td width="120"><%if (session.getAttribute("UserName")!=null)

{ out.println("<font color=red>欢迎您:"+session.getAttribute("UserName")+"</font>");}

else

{ out.println("<font color=red>请您登陆!</font>"); };%>

</td>

<%if(pageNO<=pageCount){%>

<td width="128">

<a href="../search.jsp?pagenum=<%=pageNO+1%>&keyword=<%=keyword%>&way=<%=way%>">下一</a></td>

<%}%>

<%if(pageNO>1){%>

<td width="124">

<a href="../search.jsp?pagenum=<%=pageNO-1%>&keyword=<%=keyword%>&way=<%=way%>">上一</a></td>

<%}%>

<td width="124"> </td>

<td width="124"><a href="search.jsp">查看全部热点书</a></td>

<td width="126"><a href="myorder.jsp" target="_blank">查看我的订单</a></td>

</tr>

</table>





</td>

</tr>

</table>

<table width="660" border=0 align="center" cellpadding=6 cellspacing=0 class=linehight height="123">

<%

while( RSa.next()&&qikan_num<pageRecorders)

{

String bookname= RSa.getString("书名");

String writer=RSa.getString("作者");

float sale2=RSa.getFloat("定价");

String pubname= RSa.getString("出版社");

String pubtime= RSa.getString("出版日期");

int bookid= RSa.getInt("id");

String image= RSa.getString("ISBN");



%>

<tr>

<td align=middle width="23%" height=16><a href=javascript:show("../search_info.jsp?id=<%=bookid%>","详细资料",600,300)><img src="image/hotbook/<%=image%>.jpg" border="0"></a></td>

<td valign=top width="58%" ><b>书名:</b><a href=javascript:show("../search_info.jsp?id=<%=bookid%>","详细资料",600,300)><font color="#cc0000"><b><%= bookname%></b></font></a><br>

<b><br>作者:</b><%=writer%><br>

<b>定价:</b><font color="#cc0000"> ¥<%=sale2%></font><br>

<b>出版社:</b><%=pubname%><br>

<b>出版日期:</b><%=pubtime%></td>

<td valign=bottom width="19%" height=16 >

<form name="form1<%=bookid%>" method="post" action="bookTiqiao.jsp?bookid=<%=bookid%>">

<input type="text" name="bookfield" size="3" maxlength="3" value="1">

本<img src="images/all1.jpg" width="41" height="36" onClick="submit()" style="cursor:hand">

</form>

</td>

</tr>

<tr align=middle>

<td valign=top colspan=3 height=0>

<div align="right"><img src="images/search_end.gif" width="424" height="15"></div>

</td>

</tr>

<%

qikan_num++;

}

%>

</table>

<table width="772" border="0" align="center">

<tr>

<td><%

out.println("<p><b><font color=´#174572´ size=´2´>本页有"+qikan_num+"条纪录,共找到"+pageTotal+"条</font></b></p>");

out.println("<font color=´#cc00000´ size=´2´>当前是第"+pageNO+"页</font>");

out.println("页数");

for(int i=1;i<pageCount+2;i++)

{

%>

<a href="../search.jsp?pagenum=<%=i%>&keyword=<%=keyword%>&way=<%=way%>"><u><%=i%></u></a>

<%

}

RSa.close();

stmt.close();

con.close();

}%>



</td>

</tr>

</table>

7另一种 删除数据后,跳转的方法
<%

if (session.getAttribute("UserName")==null)

{

response.sendRedirect("../index.jsp");

}else{



String keyword1,SQL;





keyword1=request.getParameter("bookid");

SQL="Delete From bookdindan Where ¶©ÔÄÕßid=´"+keyword1+"´";



Connection con=yy.getConn();

Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);

stmt.executeUpdate(SQL);

response.sendRedirect("../myorder.jsp");





stmt.close();

con.close();

}

%>

8将 年月日转换的方法 如 2004-05-02 2004年5月2日
<%!

public static String YMD(String dt)

{

try

{

return dt.substring(0,4)+"年"+dt.substring(5,7)+"月"+dt.substring(8,10)+"日";

}

catch(Exception e)

{

}

return "null";

}

%>

9 中文转换
<%!

public String getStr(String str)

{

try

{

String temp_p=str;

byte[] temp_t=temp_p.getBytes("GBK");

String temp=new String(temp_t,"ISO8859_1");

return temp;

}

catch(Exception e)

{



}

return "null";

}

10 用户注销 代码
<%

session.setAttribute("UserName","");

session.setAttribute("station","");

session.setAttribute("id","");

response.sendRedirect("default.jsp");

%>



第二篇
1 防止用户刷新页面,等待10秒
// default.jsp
<%String aaa="b"+session.getAttribute("nF");
if(!aaa.equals("bnull"))
{
session.setAttribute("nF",null);
response.sendRedirect("1.jsp");
}
else
{
session.setAttribute("nF","a");

%>
//1.jsp
<%@ page contentType="text/html; charset=gb2312"%>
<%@ page language="java" %>
<p>对不起,服务器正在运行时,请勿刷新页面,谢谢合作,如有不便敬请谅解</p>
<p>页面将于10秒后重新回到主页.......请等待!!!</p>

<%response.setHeader("Refresh","10;URL=default.jsp");%>
2用javaBean调用javascript指令
//jsp
try{
studentID=(String)session.getAttribute("studentID");
if(studentID==null){
throw new Exception();
}
}catch(Exception e){
Tools.showalert("您还没有登录,或连接超时,请重新登录!",out);
Tools.showJS("window.open(´../login.jsp´,´_self´)",out);
}
//Bean
import javax.servlet.jsp.JspWriter;
public static void showalert(String msg, JspWriter out)
{
try
{
out.print(String.valueOf(String.valueOf((new StringBuffer("<SCRIPT LANGUAGE=\"javascript\"\n> alert(\"")).append(msg).append("\");\n").append("</SCRIPT>\n"))));
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
3用javaBean 返回
public static void goback(JspWriter out)
{
try
{
out.print("<SCRIPT LANGUAGE=\"javascript\"\n> history.go(-1);\n</SCRIPT>\n");
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
4用bean 插入javascript
public static void goback(JspWriter out)
{
try
{
out.print("<SCRIPT LANGUAGE=\"javascript\"\n> history.go(-1);\n</SCRIPT>\n");
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
posted on 2005-06-07 16:22 似水流年 阅读(425) 评论(0)  编辑  收藏 所属分类: JSP/Servlet

只有注册用户登录后才能发表评论。


网站导航: