ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 210512_1~4(jsp액션태그)
    HTML_JS(Sol)/JSP(sol) 2021. 5. 12. 13:13

    액션태그

    사용자 정의의 태그

    <접두어:태그명 속성="값">태그바디 </접두어:태그명> 바디가 있는 태그는 닫히는 태그가 쌍을 이룬다.

    <접두어:태그명 속성="값" /> 바디가 없는 태그는 끝에서 마감


    include 액션태그

    <jsp:include page="경/파" />

    1) 문서를 병합한다.

    2) 각각을 처리해서 결과를 합친다. include지시어와는 반대로 작동한다.

    3) 같은이름의 변수충돌 가능성이 없다. 그래서 자주 사용하게된다.

     

     

    중요!

    <!-- include액션태그로 연결된 웹문서는 request영역을 공유한다  -->

    IncludeTag1

    <!DOCTYPE html>
    
    <head>
    <meta charset="UTF-8">
    <title>Include Tag Example1.html</title>
    </head>
    <h1>Include Tag Example1</h1>
    
    <form method=post action="IncludeTag1.jsp">
    이름:<input type="text" NAME="name"><p>
    <input type="submit" value="보내기">
    </form>
    
    <%@ page language="java" contentType="text/html; charset=UTF-8"
    	pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    
    <%
    	request.setCharacterEncoding("euc-kr");
    	String name = "Korea Football";
    %>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Include Tag Example1.jsp</title>
    
    </head>
    <body>
    <h1>Include Tag Example1</h1>
    	<!-- 액션태그 include 결과를 끌어온다 -->
    	<!-- include액션태그로 연결된 웹문서는 request영역을 공유한다  -->
    	<jsp:include page="IncludeTagTop1.jsp" />
    	include Action Tag의 Body 입니다.
    
    </body>
    </html>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    
    <title>IncludeTagTopp.jsp</title>
    </head>
    <body>
    <!-- include액션태그로 연결된 웹문서는 request영역을 공유한다  -->
    <%
    String name = request.getParameter("name");
    %>
    include Action Tag 의 Top 입니다.<p>
    <b><%=name%>Fighting!!!</b>
    <hr>
    


    IncludeTag2

     

    4) 포함되는 페이지에 파라미터를 전송할 수 있으며, request.getparameter("키명")으로 받는다

    <jsp:include page="경/파">

      <jsp:param name="키명" value="값"/>

    </jsp:include>

    	<h1>Include Tag Example2</h1>
    	<Form Method=post action="IncludeTag2.jsp">
    		SITENAME:<input type="text" name="siteName">
    		<p>
    			<input type="submit" value="보내기">
    	</Form>
    
    <%@ page language="java" contentType="text/html; charset=EUC-KR"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    
    <% 
    request.setCharacterEncoding("EUC-KR");
    String siteName = request.getParameter("siteName");
    %>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Include Tag Example2.jsp</title>
    </head>
    <body>
    <h1>Include Tag Example2</h1>
    <jsp:include page ="IncludeTagTop2.jsp">
    <jsp:param name="siteName" value="JSPStudy.co.kr"/>
    </jsp:include>
    <!-- 바디가있는 인클루드 태그 -->
    include Action Tag 의 Body 입니다.<p>
    <b><%=siteName%></b>
    <hr>
    </body>
    </html>
    <%@ page language="java" contentType="text/html; charset=EUC-KR"%>
    <!DOCTYPE html>
    
    <%
    String siteName = request.getParameter("siteName");
    %>
    include Action Tag의 top입니다.<p>
    <b><%=siteName%></b>
    <hr>


    Forward 액션태그

    <jsp:forward page="경/파"/>

     

    <jsp:forward page="경/파">바디</jsp:forward>

    해석 : 문서를 실행하다가, forward 태그를 만나면 지금까지 준비했던 출력버퍼를 버리고

            이동후 페이지를 준비해서 응답해준다.

     

    기존 : 브라우저 ---------A요청-------->A.jsp

                        <--------A응답---------

    forward 된경우 : 브라우저 -------A요청------->A.jsp --forward--> B.jsp

                                     <--------------------B응답--------------

                          특징 : 브라우저주소는 A를 가리킨다. 하지만 브라우저화면은 B를 출력한다.

                                   브라우저입장에서는 B페이지의 존재를 알수가없다.

                                    A와 B사이는 include 처럼 request영역을 공유한다.

    <h1>Forward Tag Example1</h1>
    <form method=post action="ForwardTag1_1.jsp">
    아이디: <input type="text" name="id"><p>
    패스워드: <input type="password" name="password"><p>
    <input type="submit" value="보내기">
    </form>
    
    <%@ page language="java" contentType="text/html; charset=EUC-KR"
        pageEncoding="EUC-KR"%>
    <!DOCTYPE html>
    <%
    request.setCharacterEncoding("EUC-KR");
    %>
    <html>
    <head>
    <meta charset="EUC-KR">
    <title>ForwardTag1_1.jsp</title>
    </head>
    <body>
    <h1>Forward Tag Examlple1</h1>
    Forward Tag 의 포워딩 되기 전의 페이지 입니다.
    <!-- 위에글은 출력되지않는다. -->
    <jsp:forward page="ForwardTag1_2.jsp"/>
    
    </body>
    </html>
    <%@ page language="java" contentType="text/html; charset=EUC-KR"
        pageEncoding="EUC-KR"%>
    
    <h1>Forward Tag Example1</h1>
    <%
    String id = request.getParameter("id");
    String password = request.getParameter("password");
    %>
    당신의 아이디는 <b><%=id%></b>이고<p>
    패스워드는 <b><%=password%></b>입니다.
    <!-- 출력은 이부분만 된다. -->
    


    Forward Tag

    <!DOCTYPE html>
    
    <title>ForwardTag2.html</title>
    
    <h1>Forward Tag Example2</h1>
    <form method = post action ="ForwardTag2_1.jsp">
    혈액형별로 성격 테스트<p>
    당신의이름은?<p>
    <input type="text" name="name" value="이름"><br>
    당신의 혈액형은?<p>
    <input type="radio" name="bloodType" value="A">A형<br>
    <input type="radio" name="bloodType" value="B">B형<br>
    <input type="radio" name="bloodType" value="O">O형<br>
    <input type="radio" name="bloodType" value="AB">AB형<br>
    <input type="submit" value="보내기">
    </form>
    
    <%@ page language="java" contentType="text/html; charset=EUC-KR"
    	pageEncoding="EUC-KR"%>
    	<%
    	request.setCharacterEncoding("euc-kr");
    	%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="EUC-KR">
    <title>ForwardTag2_1.jsp</title>
    </head>
    <body>
    	<h1>Forward Tag Example2</h1>
    	<%
    	String name = request.getParameter("name");
    	String bloodType = request.getParameter("bloodType");
    	%>
    	<!-- bloodType에맞는 jsp페이지로 이동시켜준다  -->
    	<jsp:forward page='<%=bloodType+".jsp"%>'>
    		<jsp:param value="<%=name%>" name="name" />
    	</jsp:forward>
    
    </body>
    </html>
    <%@ page language="java" contentType="text/html; charset=EUC-KR"
        pageEncoding="EUC-KR"%>
    
    <h1>Forward Tag Example2</h1>
    <%
    String name= request.getParameter("name");
    String bloodType=request.getParameter("bloodType");
    %>
    <b><%=name%></b>님의 혈액형은
    <b><%=bloodType %></b>형이고
    성실하고 신중하며 완벽주의자입니다.
    <%@ page language="java" contentType="text/html; charset=EUC-KR"
        pageEncoding="EUC-KR"%>
    
    <h1>Forward Tag Example2</h1>
    <%
    String name= request.getParameter("name");
    String bloodType=request.getParameter("bloodType");
    %>
    <b><%=name%></b>님의 혈액형은
    <b><%=bloodType %></b>형이고
    규격을 싫어하는 자유인입니다.
    <%@ page language="java" contentType="text/html; charset=EUC-KR"
        pageEncoding="EUC-KR"%>
    
    <h1>Forward Tag Example2</h1>
    <%
    String name= request.getParameter("name");
    String bloodType=request.getParameter("bloodType");
    %>
    <b><%=name%></b>님의 혈액형은
    <b><%=bloodType %></b>형이고
    강한 의지의 소유자입니다.
    <%@ page language="java" contentType="text/html; charset=EUC-KR"
        pageEncoding="EUC-KR"%>
    
    <h1>Forward Tag Example2</h1>
    <%
    String name= request.getParameter("name");
    String bloodType=request.getParameter("bloodType");
    %>
    <b><%=name%></b>님의 혈액형은
    <b><%=bloodType %></b>형이고
    정확한 판단력을 가진 합리주의자입니다.

    이름넣어서 parameter로 보내기 추가


    참고)

    받을때 1)

    <% // 요소명이 같은것이 request 영역에 여러개 있을때

     String[] name = request.getParameterValues("my");

    for(int i=0; i<name.length; i++){

    out.print(name[i]+"<br>");

    }

    %>

    배열화시켜서 반복문돌린다.

    <%@ page language="java" contentType="text/html; charset=EUC-KR"
        pageEncoding="EUC-KR"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="EUC-KR">
    <title>Insert title here</title>
    </head>
    <body>
    <!-- 전송방식을 지정하지 않으면 get방식이 디폴트다. -->
    <form action="received2.jsp">
    	<input type="text" name="my" value="my1"><br>
    	<input type="text" name="my" value="my2"><br>
    	<input type="text" name="my" value="my3"><br>
    	<input type="text" name="my" value="my4"><br>
    	<input type="text" name="my" value="my5"><br>
    	<input type="submit" value="전송합니다."><br>
    </form>
    </body>
    </html>
    <%@page import="java.util.Enumeration"%>
    <%@ page language="java" contentType="text/html; charset=EUC-KR"
    	pageEncoding="EUC-KR"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="EUC-KR">
    <title>Insert title here</title>
    </head>
    <body>
    	<%//요소명 같은것이 여러개 일때
    String[] name= request.getParameterValues("my");
    for(int i=0; i< name.length; i++){
    out.print(name[i]+"<br>");
    }
    
    %>
    
    </body>
    </html>

     

    받을때 2)

    요소명을 모를때

    <%

    Enumeration ename = requset.getParameterNames();

    while(ename.hasMoreElements()){

    String key = (String) ename.nextElement();

    out.print(key);

    String myvalue =

    <%@ page language="java" contentType="text/html; charset=EUC-KR"
        pageEncoding="EUC-KR"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="EUC-KR">
    <title>Insert title here</title>
    </head>
    <body>
    <!-- 전송방식을 지정하지 않으면 get방식이 디폴트다. -->
    <form action="received3.jsp" method="post">
    	<input type="text" name="my1" value="my1111"><br>
    	<input type="text" name="my2" value="my2222"><br>
    	<input type="text" name="my3" value="my3333"><br>
    	<input type="text" name="my4" value="my4444"><br>
    	<input type="text" name="my5" value="my5555"><br>
    	<input type="submit" value="전송합니다."><br>
    </form>
    </body>
    </html>
    <%@page import="java.util.Enumeration"%>
    <%@ page language="java" contentType="text/html; charset=EUC-KR"
        pageEncoding="EUC-KR"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="EUC-KR">
    <title>Insert title here</title>
    </head>
    <body>
    <%
    //요소명을 모를떄
    Enumeration ename = request.getParameterNames();
    
    while(ename.hasMoreElements()){
    	String key = (String) ename.nextElement();
    	out.print(key);
    	String myvalue = request.getParameter(key);
    	out.print(myvalue+"<br>");
    }
    %>
    </body>
    </html>

    autoFlush

    <%@ page language="java" contentType="text/html; charset=EUC-KR"
        pageEncoding="EUC-KR" buffer="1kb" autoFlush="true"%>
        <!-- autoFlush false로하면 버퍼오버플로우로 error -->
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="EUC-KR">
    <title>Insert title here</title>
    </head>
    <body>
    <%
    // buffer="1kb" autoFlush="true" 1kb가 차면 자동으로 내용을 전송함
    for(int i =1; i<=1000; i++){
    	int size = out.getBufferSize();
    	int size2 = out.getRemaining();
    	out.print(i+". 무궁화꽃이피었습니다"+size+
    			"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+":"+ size2 +"<br>");
    }
    %>
    </body>
    </html>

Designed by Tistory.