当前位置: 移动技术网 > IT编程>开发语言>Java > JSP的login程序代码

JSP的login程序代码

2017年12月12日  | 移动技术网IT编程  | 我要评论

蔡振华赞女排精神,日不落英文歌词,导播网

jsp的login程序代码

<!-- login manager -->
<java type="class">

public static boolean checkuserpermission(httpsession mysession, httpservletrequest request, httpservletresponse response)
{
if (mysession.getvalue("haspermission") == null || !((boolean) mysession.getvalue ("haspermission" )).booleanvalue())
{
string requestedurl = httputils.getrequesturl(request).tostring();
string querystring = request.getquerystring();
if (querystring != null)
{
requestedurl = requestedurl + "?" + querystring;
}
requestedurl = response.encodeurl(requestedurl);
mysession.putvalue("requestedurl", requestedurl);
return false;
}
else
{
return true;
}
}

</java>

<%
// jsp文件
string gooduser = "bill";
string goodpass = "bobo";
httpsession mysession = request.getsession(true);
string errormessage = "please login to access the page you requested";
boolean loginattempt = false;
string mymethod = request.getmethod();
if (request.getparametervalues("click") != null && request.getparametervalues ("click")[0].trim().equals("log in"))
{
loginattempt = true;
}

if (loginattempt)
{
string username = request.getparametervalues("user")[0].trim();
string password = request.getparametervalues("pass")[0].trim();
//out.println("username = |" + username + "| & password = |" + password + "|<br>");
if (gooduser.equals(username) && goodpass.equals(password))
{
response.sendredirect((string)mysession.getvalue("requestedurl"));
mysession.putvalue("haspermission", new boolean(true));
errormessage = "unable to redirect: " + (string) mysession.getvalue("requestedurl");
}
else
{
errormessage = "you did not get the username or password right";
}
}
else
{
errormessage = "haven't tried logging in yet.";
if (mysession.getvalue("requestedurl") == null)
{
mysession.putvalue("requestedurl", "/index.jsp");
}
//out.println("set userreferrer to " + mysession.getvalue("redirectto") + "<br>");
}
%>

<center>
<font color=red><%=errormessage%></font>
<table align=center>
<form action="adminlogin2.jsp" method=post name="login">
<tr>
<td>username:</td>
<td><input type=text name=user value=""></td>
</tr>
<tr>
<td>password:</td>
<td><input type=password name=pass value=""></td>
</tr>
<tr>
<td colspan=2 align=center><input type=submit name=click value="log in"> </td>
</tr>
</form>
</table>
</center>
original url: <%= (string)mysession.getvalue("requestedurl") %>
<script language="javascript">
// <!--
if (document.forms.login != null)
document.forms.login.user.focus();
// -->
</script>

  5.11 checkbox在jsp中的使用方法



<%@ page language="java" contenttype="text/html" %>
<%@ page import="com.ora.jsp.util.*" %>
<html>
<body bgcolor="white">
<form action="checkbox.jsp">
<input type="checkbox" name="fruits" value="apple">apple<br>
<input type="checkbox" name="fruits" value="banana">banana<br>
<input type="checkbox" name="fruits" value="orange">orange<br>
<input type="submit" value="enter">
</form>

<%
string[] picked = request.getparametervalues("fruits");
if (picked != null && picked.length != 0) {
%>
you picked the following fruits:
<form>
<input type="checkbox" name="fruits" value="apple"
<%= arraysupport.contains(picked, "apple")?"checked" : "" %> >apple<br>
<input type="checkbox" name="fruits" value="banana"
<%= arraysupport.contains(picked,"banana")?"checked":"" %> >banana<br>
<input type="checkbox" name="fruits" value="orange"
<%= arraysupport.contains(picked,"orange")?"checked" : "" %> >orange<br>
</form>
<% } %>
</body>
</html>

  5.12 request对象

  ·如何获得一个运行时刻的jsp/servlet文件的绝对url地址

string url = request.getrequesturl();
if (request.getquerystring() != null)
{
url += '?' + request.getquerystring();
}
url theurl = new url ( request.getscheme() , request.getservername() , request.getserverport(),url);
out.print(url.tostring());

  ·如何知道客户端通过哪个url访问本页面

string callingpage = request.getheader("referer");
out.print(callingpage);

  ·如果form中出现好几个submit按钮,jsp/servlet如何判断哪个按钮进行了提交在form中可以这样定义:

<input type=submit name="name" value="john"><br>
<input type=submit name="name" value="joe"><br>
在jsp/servlet中使用request.getparameter("name"),根据返回值就可进行判断。

  5.13 include指令

  这个指令让你可以在jsp编译成servlet的时候可以包含进一些文件。这个指令是这样的:

<%@ include file="relative url" %>

  这个指定的url通常是指向它的jsp页面的相关解释。包含的文件内容被当作jsp文本来分析,因此可以包含静态 html、scripting elements、directives以及actions。

  例如,很多站点的每个页面上都包含有小的导航条。这个 include 是做这个的很好方法,省得开发者经常拷贝html到不同的文件中。例如:

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>servlet tutorial: javaserver pages (jsp) 1.0</title>
<meta name="author" content="webmaster@somesite.com">
<meta name="keywords" content="...">
<meta name="description" content="...">
<link rel=stylesheet href="site-styles.css" type="text/css">
</head>
<body>
<%@ include file="/navbar.html" %>
<!-- part specific to this page ... -->
</body>
</html>

  既然文件是在页面被编译的时候插入的,如果导航条改变了,你需要去重新编译它所指向的所有jsp页面。注意,问题很容易在这里出现。一些读者在改变导航条后,重新运行包含导航条的jsp文件时,发现导航条不变。原因有两个:一是前面提到的include指令在jsp编译成servlet的时候包含进导航条文件;第二就是jsp文件是被编译成servlet之后再运行的,如果服务器发现jsp文件没有被修改,则直接去调用已经编译好的servlet。如此一来,当调用jsp文件时,由于直接调用了编译好的servlet,显示的结果当然就是以前的导航条了。只要稍微修改一下jsp文件,该问题就可以自行解决。

  如果导航条不是经常改变的,而且想要整个过程尽可能高效,那么在这样的环境下这是好的妥协。 如果这个 included 文件经常改变,那么建议读者使用用 jsp:include 行为(action)来代替。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网