之前使用过html,jsp,servlet来进行相应的功能实现,现在需要把servlet中的代码整合到jsp代码中实现同样的功能。
jsp是一种动态页面技术,主要的目的是将表示逻辑从Servlet中分离出来。当jsp页面被客户访问时,页面首先在服务器端被转换成一个Java源程序文件,然后改程序在服务器端编译和执行,最后向客户端发送执行结果,通常是文本数据。所以在jsp中编写相应符合jsp语句格式的代码,其实和servlet中的代码出入不大。
下面以登陆为例进行学习。
- 首先我们还是编写一个登陆页面的jsp文件,将form表单提交到login.jsp进行登陆验证:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>loginpage</title>
</head>
<!-- 页面的样式文件 -->
<style type="text/css">
body{
background-color : #eee;
}
#first{
width:500px;
margin:30px auto;
}
</style>
<body>
<div id="first">
<form action="login.jsp" method="post" >
<table>
<tr>
<td>username</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>password</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td><input type="submit" value="submit"></td>
<td><input type="reset" value="cancel"></td>
</tr>
</table>
</form>
</div>
</body>
</html>- 我们在login.jsp中进行登陆验证:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="UTF-8" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String username = request.getParameter("username").trim();
String password = request.getParameter("password").trim();
out.println("<html><body>");
if("admin".equals(username) && "1234".equals(password)){
out.println("欢迎来到系统"+username);
// response.sendRedirect("select.jsp");
}
else{
out.println("sorry");
request.getRequestDispatcher("NewFilelogin.jsp");
}
out.println("</body></html>");
%>
</body>
</html>- 进行验证:
说明在jsp中也能够进行相应的登陆验证。
本文介绍如何将Servlet中的登录验证代码整合到JSP中实现相同功能。通过示例展示如何创建登录表单并使用JSP进行验证。

4233

被折叠的 条评论
为什么被折叠?



