「java」Bubble Sortバブルソートアルゴリズムのコード

javaコード:
public class BubbleSort {
public static void bubble(int array[]) {
boolean change = true;
for (int i = 0; i < array.length && change; i++) {
change = false;
for (int j = 0; j < array.length – i – 1; j++) {
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
change = true;
}
}
}
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + ” “);
}
System.out.println(“\n”);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int array[] = { 58, 42, 35, 77, 55, 24, 35, 98 };
bubble(array);
}
}

「Java」選択ソート(Selection Sort)アルゴリズムを実装するサンプルコード

Javaコード
public class SelectSortAna {
static int i;
static int temp;
public static void selectSort(int array[]) {
for (i = 0; i < array.length; i++) {
int k = i;
for (int j = i + 1; j < array.length; j++) {
if (array[j] < array[k]) {
k = j;
}
}
if (k != i) {
temp = array[i];
array[i] = array[k];
array[k] = temp;
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int array[] = { 28, 42, 55, 87, 35, 44, 25, 89 };
selectSort(array);
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + ” “);
}
System.out.println(“\n”);
}
}

「java」map.keyset().iterator()の使い方

Javaコード
Iterator<String> map = map.keySet().iterator();
while(map.hasNext()){
System.out.println(map.next());
}

「Java」Documentオブジェクトを文字列Stringに変換するコード

Javaコード
public static String toXMFuncL(Document xmlDoc, String encoding) throws IOException
{
ByteArrayOutputStream byteRs = new ByteArrayOutputStream();
PrintWriter out=new PrintWriter(byteRs);
Format format = Format.getPrettyFormat();
format.setEncoding(encoding);
XMLOutputter docWriter = new XMLOutputter(format);
try {
docWriter.output(xmlDoc, out);
} catch (Exception e) {
}
return byteRs.toString();
}

nginxサーバの設定についてまとめ

サンプルコード
if (!-e $request_filename) {
proxy_pass http://sample.com;
}

if ($http_user_agent ~ MSIE) {
rewrite ^(.*)$ /nginx-ie/$1 break;
}

location ~* \.(gif|jpg|png|swf|flv|swf)$ {
valid_referers none blocked www.sample.com sample.com;
if ( $invalid_referer ) {
rewrite ^/ http://$host/logo.png;
}
}

//有効期間
location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
if ( -f $request_filename ) {
expires 1h;
break;
}
}

//ディレクトリアクセス禁止
location ~* \.(txt|doc)${
root /data/www/wwwroot/linuxtone/test;
deny all;
}

「nginx」リダイレクトrewrite設定のメモ

設定内容
server {
listen 80;
server_name sample.com www.sample.com;
root /home/web;
if($host !~ “www\.sample\.com$”) {
rewrite ^(.*) http://www.sample.com$1 permanent;
}
}
nginx再起動
/etc/init.d/nginx restart

「jQuery」ajaxでてpost送信データ取得するコード

サンプルコード
function doCftPostFunc()
{
var title=encodeURI(encodeURI(document.getElementById(“title”).value));
var content=encodeURI(encodeURI(document.getElementById(“content”).value));
var url=”writeBlg.jsp”;
var postDate=’title=’+title+”&content=”+content;
httpRequest=createRequest();
httpRequest.open(“POST”,url,true);
httpRequest.onreadystatechange=disContent;
//ヘッダー送信
httpRequest.setRequestHeader(“content-length”,postDate.length);
httpRequest.setRequestHeader(“Content-Type”,”application/x-www-form-urlencoded”);
httpRequest.send(postDate);
}
function disContentFunc()
{
if(httpRequest.readystate==4)
{
if(httpRequest.status==200)
{
var response=httpRequest.responseText;
//indexOf()メソッド
if(response.indexOf(“operateOK”)>0)
{
document.getElementById(“addButton”).disabled=false;
}
var title=document.getElementById(“title”).value;
var content=document.getElementById(“content”).value;
document.getElementById(“totalContent”).innerHTML+=”<br>”+”title:”+title+”<br>”+”content:”+content;
document.getElementById(“title”).value=””;
document.getElementById(“content”).value=””;
}
else
{
alert(‘Something Wrong has Happend’);
}
}
}

「Java」クッキーCookieを取得するコード

//COOKIEの取得
public String getCookieValue(Cookie[] cookies, String cookieName) {
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
if (cookieName.equals(cookie.getName())) {
return (cookie.getValue());
}
}
return null;
}

「JavaScript」クッキーcookieの読み・書きコード

//cookieを読む
function readCookie(name)
{
var cookieValue = “”;
var search = name + “=”;
if(document.cookie.length > 0)
{
offset = document.cookie.indexOf(search);
if (offset != -1)
{
offset += search.length;
end = document.cookie.indexOf(“;”,offset);
if (end == -1) end = document.cookie.length;
cookieValue = unescape(document.cookie.substring(offset, end))
}
}
return cookieValue;
}
//cookieを書く
function writeCookie(name, value, hours)
{
var expire = “”;
if(hours != null)
{
expire = new Date((new Date()).getTime() + hours * 3600000);
expire = “; expires=” + expire.toGMTString();
}
document.cookie = name + “=” + escape(value) + expire;
return cookieValue;
}

「struts2」セッションを削除する方法まとめ

方法1(SessionAwareを継承してsessionを取得、invalidate())
public class ClearAction extends ActionSupport implements SessionAware{
@Override
public String execute() throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session22 = request.getSession();
session22.invalidate();
return super.execute();
}
public void setSession(Map arg0) {
}
}

方法2(ActionContextでsessionを取得して、clear())
public class ClearAction extends ActionSupport{
@Override
public String execute() throws Exception {
ActionContext ac = ActionContext.getContext();
Map session = ac.getSession();
session.clear();
return super.execute();
}
}

方法3
public class ClearAction extends ActionSupport{
@Override
public String execute() throws Exception {
ActionContext ac = ActionContext.getContext();
Map session = ac.getSession();
session.remove(“bbuser”);
session.remove(“gguser”);
session.remove(“ffuser”);
return super.execute();
}

}