// Cast, 형변환 , valueOf , parseInt


* String to int / int to String

public class Ex0001 {

    public static void main(String[] args) {

 

// valueOf()는 음수를 인식하지 못함.

        

// parseInt()는 음수를 인식함.

 

        String a = "1";

        int b = 2;

  

        int temp = b + Integer.valueOf(a); //String형을 Int형으로 형변환 1 + 2

        int temp1 = b + Integer.parseInt(a); //String형을 Int형으로 형변환 1 + 2 

 

        String temp2 = a + String.valueOf(b); //Int형을 String형으로 형변환 1+2 

        String temp3 = a + Integer.toString(b); //Int형을 String형으로 형변환 1+2

    }

}


* 나중에 다시 추가 하여 정리하나 기본적인 내용이니 까먹지말자...



request.setAttribute() 와 request.getAttribute()


request.setParameter() 와 getParameter()를 이용하면 String의 값 밖엔 받을 수 없다. 


List를 받기 위해서는 setAttribute()와 getAttribute()를 써야 한다.

 이때 type이 Object 이기 때문에 반드시 형변환을 해줘야 한다.

 

혹시나 해서 문법도 쓴다.

 action에서 객체를 request에 담을 때.

request.setAttribute("객체명", 객체);

 

이렇게 해서 jsp를 호출하면 jsp에서 "객체명"을 이용해서 객체를 받을 수 있다.


< %

Object x = request.getAttribute("객체명");

% >

 

 > Object 형으로 받는 다는 것. 핵심 포인트.




* java to jsp : 


java: 

String str = "안녕하세요"

int num = 100;

request.setAttribute("STR",str);   //Attribute 에 호출명 STR 로 저장 

request.setAttribute("NUM",num);   //Attribute 에 호출명 NUM 로 저장

jsp : ${STR} // ${NUM}     

출력 화면: 안녕하세요 100


* jsp to java : 



function fn_submit(){

var form = document.test;

form.action = "addressSearch.do";

form.submit();

}

jsp: <input type="button" value="서브밋----" onclick="fn_submit()" />

     <form name="test" id="test" method="post">

<input type="text" name="test1" value="testtest11" />

<input type="text" name="test2" value="testtest22" />

<input type="text" name="test3" value="testtset33" />

     </form>


jsp: ajax : url: java.do? 에 값을 붙여준다.



java: String testx1 = request.getParameter("test1");

         String testx2 = request.getParameter("test2");

         String testx3 = request.getParameter("test3");



ajax post(json) 형식 값넘길때 한글 깨짐 현상.


JSP

var inputValue = $("#addressSearchBox").val();

inputValue = escape(encodeURIComponent(inputValue));


JAVA

String searchValue = java.net.URLDecoder.decode(request.getParameter("searchValue"),"UTF-8");     


++++

String alpha = LeftMenu.getAlpha(); //클래스명.함수명();

System.out.println(alpha);


public class LeftMenu {

public static String getAlpha() throws DoException {

//static 변수형 함수명(받는값);

String def = "get!!!";

return def;//리턴

}


++


파라메터 받기

String addressType =  request.getParameter("addressType"); // 지번,도로명 라디오버튼 값

String authority_code = "40"; //권한 osp:20, sp:30, cp:40


값 저장

request.setAttribute("authority_code", authority_code);


import java.net.*;

import java.io.*;


public class TestURLConnection {

    public static void main(String[] args) {

        URL url = null;

        URLConnection urlConnection = null;

       

        // URL 주소

        String sUrl = "http://localhost/test.jsp";


        // 파라미터 이름

        String paramName = "animal";


        // 파라미터 이름에 대한 값

        String paramValue = "dog";


 


        try {

            // Get방식으로 전송 하기

            url = new URL(sUrl + "?" + paramName + "=" + paramValue);

            urlConnection = url.openConnection();

            printByInputStream(urlConnection.getInputStream());

           

            

            

            // Post방식으로 전송 하기

            url = new URL(sUrl);

            urlConnection = url.openConnection();

            urlConnection.setDoOutput(true);

            

            

            printByOutputStream(urlConnection.getOutputStream(), paramName + "=" + paramValue);

            printByInputStream(urlConnection.getInputStream());

            

            

            

            

            

        } catch(Exception e) {

            e.printStackTrace();

        }

    }

   

    // 웹 서버로 부터 받은 웹 페이지 결과를 콘솔에 출력하는 메소드

    public static void printByInputStream(InputStream is) {

        byte[] buf = new byte[1024];

        int len = -1;

       

        try {

            while((len = is.read(buf, 0, buf.length)) != -1) {

                System.out.write(buf, 0, len);

            }

        } catch(IOException e) {

            e.printStackTrace();

        }

    }


 


    // 웹 서버로 파라미터명과 값의 쌍을 전송하는 메소드

    public static void printByOutputStream(OutputStream os, String msg) {

        try {

            byte[] msgBuf = msg.getBytes("UTF-8");

            os.write(msgBuf, 0, msgBuf.length);

            os.flush();

        } catch(IOException e) {

            e.printStackTrace();

        }

    }



* 두번이나 당해서 정리를 해둬야겠다..

화면에서 파라메터로 넘오어오는 String형은 비교문시 안되면 trim() 으로 잘라주자.....



// equals(비교)

if("감사합니다.".equals(param.getContents_type().trim())){


}


// 값이 없지 않을때.

if(!"".equals(resultVo.getProduction_language().trim())  && !"null".equals(resultVo.getProduction_language())){

productLanguage = channelManageService.getProductLanguage(resultVo);

}

+ Recent posts