`
收藏列表
标题 标签 来源
java系统中的百分数使用 java
import java.text.NumberFormat;

public class NumFormat {

	public static void main(String[] args) {
		double a=1;
		double b=3;
		System.out.println(percent(a, b));
	}
	public static String percent(double num1,double num2){//创建获取两个数的百分比芳芳
		String str = "";//创建字符串对象
		double num3 = num1 / num2;//获取两个参数的商
		NumberFormat numFormat = NumberFormat.getPercentInstance();//返回当前默认语言环境的百分比格式
		numFormat.setMinimumFractionDigits(2);//设置百分数的小数部分所允许的最小位数,如果将参数设置为负数,则使用0
		str = numFormat.format(num3);//格式规范
		return str;
	}
}
servlet登录拦截器 java
烂机器一般主要用在网页设计中,主要过滤后台,防止没有登录的用户对数据进行乱改乱添加,不过对前台的页面一般还是可以访问的,因为前台主要是一些超链接,和一些action请求。如:
	<!--  登录过滤器  -->
	<filter>
		<filter-name>onlineFilter</filter-name>
		<filter-class>
			com.seosum.infosub.util.OnlineFilter
		</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>onlineFilter</filter-name>
		<url-pattern>/pages/back/*</url-pattern>
	</filter-mapping>


--------------------------------------------------------------------------------------------

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class OnlineFilter extends HttpServlet implements Filter{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		// 这里设置如果没有登录将要转发到的页面
		RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");
		
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		HttpSession session = req.getSession(true);
		 
		String username = (String)session.getAttribute("username");
		if(username == null || "".equals(username.trim())){
			// 跳转到登录页面
			dispatcher.forward(request, response);
			res.setHeader("Cache-Control", "no-store");
			res.setDateHeader("Expires", 0);
			res.setHeader("Pragma", "no-cache");
		}else{
			chain.doFilter(request, response);
		}
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
		
	}

}


<!--  登录过滤器  -->
	<filter>
		<filter-name>onlineFilter</filter-name>
		<filter-class>
			com.huawei.filter.util.OnlineFilter
		</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>onlineFilter</filter-name>
		<url-pattern>/pages/*</url-pattern>//过滤掉拥有在pages下的页面,不过滤action
	</filter-mapping>




<struts>
    	<package name="login" extends="struts-default">
    		<action name="add" class="com.huawei.filter.action.LoginAction" method="addLogin">
    			<result name="success" type="redirect">list.action</result>
    			<result name="fail">/pages/error.jsp</result>
    		</action>	
    		<action name="list" class="com.huawei.filter.action.LoginAction" method="listLogin">
    			<result name="success">/pages/listlogin.jsp</result>
    		</action>	
    	</package>
    </struts>
System.setProperty和System.getProperty java
public static void main(String[] args) {
		System.setProperty("a", "nihao");//设置一个属性
		String pro= System.getProperty("a");
		System.out.println(pro);//获取自己设置的一个属性
		System.out.println(System.getProperty("file.separator"));//获取当前系统中文件的分隔符
		System.setProperty("homePage","http://" + Utils.getServerIp()+":" + PropertysConstants.TOMCAT_PORT + PropertysConstants.ACCESS_PATH);
	}




<%@ page language="java" contentType="text/html; charset=GBK" isErrorPage="true" pageEncoding="GBK"%>


<html>
<body>
<font size="1">找不到网页 </font>
<br>
<font size="1">您要查看的网页可能已被删除、名称已被更改,或者暂时不可用。</font>
<br> 

--------------------------------------------------------------------------------
<br> 

<font size="1">请尝试以下操作:</font>
<br>
<font size="1">如果您已经在地址栏中输入该网页的地址,请确认其拼写正确。</font>
<br>
<ul>
	<li><font size="1">打开<a href= "<%=System.getProperty("homePage")%>" target="_top"><%=System.getProperty("homePage")%></a>主页,然后查找指向您感兴趣信息的链接。</font></li> 
	<li><font size="1">单击后退按钮,尝试其他链接。</font></li> 
	<li><font size="1">单击搜索,寻找 Internet 上的信息。</font></li>
</ul>

<font size="1">HTTP 404 - 未找到文件</font>
<br> 
<font size="1">Internet Explorer</font>
</body>
</html> 
 
获取java类绝对路径 java
private String getWorkPath(){
		URL root = this.getClass().getResource("/");
		String path =null;
		try {
			path = URLDecoder.decode(root.getPath(), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		if(path.startsWith("jar:")){
			path = path.substring(4, path.length());
		}
		StringBuffer sb = new StringBuffer();
		String[] temp = path.split("/");	
		for( int i=1; i<temp.length; i++ ) {
           if( temp[i].indexOf("!") == -1 ) {
             sb.append( temp[i] + "/" );
           }
           else {
             break;
           }
         }
		path = sb.toString();
		return path;
	}
Global site tag (gtag.js) - Google Analytics