几个简单的JSP网页以及注意点

今天学习JSP,进行了几个页面的实践,发现了几个要在平常开发中注意以及避免的问题。还有一些小知识点的总结。

1、用小脚本在页面显示一个九九乘法表,并且带上鼠标样式。

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index2.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

	<style>
		.red{
		background-color:yellow;
		color:red;
		
		}
		.white{
		backgound-color:white;
		color:#000000;
		
		}
	</style>
  </head>
  
  <body>
    	<table border="1">
    	<%
    	//九九乘法表
    	for(int i=1;i<=9;i++){
    	%>
    	<tr>
    	<%
    		for(int j=1;j<=i;j++){
    		%>
    		<td οnmοuseοver="this.className='red'" οnmοuseοut="this.className='white'">
    			<%=i%>*<%=j%>=<%=i*j%></td>
    		<%} %>
    		</tr>
    		<%} %>
    		</table>
  </body>
</html>

2、 <%@include file="header.jsp" %>类似如此的叫静态包含,就是包含的java源代码,就是将两个页面的java编译后后再合并到一起,然后编译,缺点就是变量名容易冲突。

3、我们用request传值过来之后需要进行编码转换:request.setCharacterEncoding("UTF-8");

4、获取用户从客户端传过来的值

方法一:单个值我们用request.getParameter("name");

方法二:getParameter 只能取单个参数值,如果一个参数有多个参数值,就要采用 String[]getParameterValues();String[] ins=request.getParameterValues("name");

方法三:以上的方法与前段的页面太耦合,需要name都一样,违背了“高内聚低耦合”的特点,所有采用以下方法得到参数名然后得到参数值的方法

<%
         request.setCharacterEncoding("utf-8");
    //取出所有的参数的名字
    Enumeration<String> parameternames= request.getParameterNames();
    
    while(  parameternames.hasMoreElements()  ){
    		String pn=parameternames.nextElement();     //取出每一个参数的名字(键)
    	    if(  	request.getParameterValues(pn).length>1){
    	    	String[] values=request.getParameterValues(pn);
    	    	out.println( pn+"=");
    	    	for( String s:values){
    	    		out.println( s +" ");
    	    	}
    	    }else{
    	    	String value=request.getParameter(pn);
    	    	out.println(  pn+"="+value+"<br />");
    	    }
    }
    %>
5、<%!   %> 在声明中只能有全局变量定义,及方法的定义。

6、两种注释:

<!--  html注释: 是会返回到客户端浏览器上去的,即它要消耗流量 ,    对静态代码使用注-->
<%--  服务器注释,只要服务器中有,不会消耗流量  --%>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,在获取微信公众号网页授权和用户信息之前,我们需要先了解微信公众号的几个概念: 1. AppID和AppSecret:AppID是每个公众号独有的身份标识,在开发者中心中可查看;而AppSecret则是用于获取access_token的密钥。 2. 授权作用域:微信网页授权分为静默授权和弹出授权,前者只能获取用户基本信息,后者可以获取用户详细信息。 3. 网页授权流程:用户同意授权后,微信后台会将授权code返回给开发者,开发者可以使用该code获取access_token,并通过access_token获取用户信息。 接下来,我们就可以开始使用JSP获取微信公众号网页授权和用户信息了。 1. 获取授权链接 首先,我们需要构造出授权链接,以便用户击授权。 String redirect_uri = URLEncoder.encode("http://your.domain.com/callback.jsp", "UTF-8"); String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appId + "&redirect_uri=" + redirect_uri + "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect"; 其中,redirect_uri是授权后重定向的回调链接地址,需要进行URLEncode编码;scope=snsapi_userinfo表示需要用户授权获取详细信息。 2. 获取授权code 当用户击授权后,需要在回调页面(上面代码中的callback.jsp)中获取授权code。 String code = request.getParameter("code"); 3. 获取access_token 利用上一步获取的授权code,构造出获取access_token的链接,并向该链接发送HTTP GET请求,获取access_token。 String tokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appId + "&secret=" + appSecret + "&code=" + code + "&grant_type=authorization_code"; URL url = new URL(tokenUrl); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.connect(); InputStream inputStream = urlConnection.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String line; StringBuffer stringBuffer = new StringBuffer(); while ((line = bufferedReader.readLine()) != null) { stringBuffer.append(line); } JSONObject json = new JSONObject(stringBuffer.toString()); String accessToken = json.getString("access_token"); String openid = json.getString("openid"); 4. 获取用户信息 利用获取到的access_token和openid,可以构造出获取用户信息的链接。 String userInfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=" + accessToken + "&openid=" + openid + "&lang=zh_CN"; URL url = new URL(userInfoUrl); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.connect(); InputStream inputStream = urlConnection.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String line; StringBuffer stringBuffer = new StringBuffer(); while ((line = bufferedReader.readLine()) != null) { stringBuffer.append(line); } JSONObject json = new JSONObject(stringBuffer.toString()); String nickname = json.getString("nickname"); String headimgurl = json.getString("headimgurl"); 以上就是使用JSP获取微信公众号网页授权和用户信息的流程。需要注意的是,获取用户信息需要用户同意授权获取详细信息,并且在构造链接时需要进行URL编码。另外,在实际开发过程中,建议将获取access_token和获取用户信息的代码封装为工具类,以便重复使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值