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="Content-Type" content="text/html; charset=utf-8">
<title>jsp语法</title>
</head>
<body>
         <!--   三大指令:
         page
             language = java,此页面服务器代码指定为JAVA
             contentType,指定页面字符编码
             pageEncoding 服务器字符编码
         ,include,taglib -->
         html + css + javascript : 客户端语言(直接用浏览器就能打开的)
         java:服务器语言(直接用浏览器打开无法运行,所以要用服务器软件解析(Tomcat 将jsp翻译成浏览器能))
         //如何在界面HTML里面使用服务器代码(JAVA)
          //1.  <%  %>     2. <div><%=  %></div>  3.定义成员方法  <%!  %>
         

</body>
</html>

一个简单的跳转页面

<%@ 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="Content-Type" content="text/html; charset=UTF-8">
<title>登录界面</title>
</head>
<body>
	<!-- 表单数据提交到哪个地方action(服务器页面)去 
		method="get/post",提交数据的方式
		get(默认):  1.地址栏上可以看见提交的数据 2.一般提交的数据最大为1K
		post: 1.地址栏上信息不可见 2.提交的数据可以超过1K
	-->
	<form action="LoginServlet.jsp" method="post">
		请输入帐号:<input type="text" name="userid"/>
		<%
			if(request.getAttribute("msg")!=null){
		%>
		<div><%=request.getAttribute("msg").toString() %></div>
		<%} %>
		<br/>
		请输入密码:<input type="password" name="userpwd"/><br/>
		<input type="submit" value="登录"/>
	</form>
</body>
</html>

跳转LoginServlet.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="Content-Type" content="text/html; charset=UTF-8">
<title>接收客户端数据页面</title>
</head>
<body>
	<%
		//接收页面传递过来的数据
		//JSP内置对象之请求对象1.request
		
		//接收帐号信息
		String strid = request.getParameter("userid");
	
		//接收密码信息
		String strpwd = request.getParameter("userpwd");
		
		//JSP内置对象之打印对象2.out
		//out.println("帐号是:"+strid);
		//out.println("密码是:"+strpwd);
		
		//JSP内置对象之响应对应3.response
		if(strid.equals("admin")&&strpwd.equals("123456")){
			response.sendRedirect("index.jsp");//地址重定向
		}else{//登录失败
			//response.sendRedirect("login.jsp");//返回登录页面,不带数据过去
			
			request.setAttribute("msg", "登录失败");//带数据,在LoginServlet定义一个变量msg,值 为"登录失败"
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}
	%>
</body>
</html>

 

网页访问原理

 

 

PS:局网对象  request.setAttribute("","")

      全网对象   session.setAttribute("","")

session获取值直接用 ${" "}  使用前需要导入jstl架包

并加入代码

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
//使用jstl架包需要加入的配置
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值