[ex]
a.jsp
=====
<%@ page contentType="text/html;charset=euc-kr" %>
<%%>
<a href="c/b.jsp?a=1&b=2&c=3">문서</a>
[an1]
ap1/c/b.jsp
===========
<%@ page contentType="text/html;charset=euc-kr" %>
<%
String s1=request.getParameter("a");
String s2=request.getParameter("b");
String s3=request.getParameter("c");
out.println(s1+"<br>");
out.println(s2+"<br>");
out.println(s3+"<br>");
%>
[an2]
ap1/c/b.jsp
===========
<%@ page contentType="text/html;charset=euc-kr" %>
<%=request.getParameter("a")%><br>
<%=request.getParameter("b")%><br>
<%=request.getParameter("c")%><br>
====================================================================================================
[ex] 실행해보기
a.jsp
=====
<%@ page contentType="text/html;charset=euc-kr" %>
<form action="b.jsp">
<input type="text" name="t1">
<input type="submit">
</form>
b.jsp
======
<%@ page contentType="text/html;charset=euc-kr" %>
<%=request.getParameter("t1")%>
====================================================================================================
[ex] 전송버튼을 클릭하면 체크박스에 선택된 값이
화면에 출력될 수 있도록 소스코드를 작성하시오.
a.jsp
=====
<%@ page contentType="text/html;charset=euc-kr" %>
<form action="b.jsp">
<input type="checkbox" name="ch" value="javascript">javascrip
<input type="checkbox" name="ch" value="java">java
<input type="checkbox" name="ch" value="oracle">oracle
<input type="checkbox" name="ch" value="xml">xml
<input type="checkbox" name="ch" value="jquery">jquery
<input type="submit">
</form>
[an]
b.jsp
=====
<%@ page contentType="text/html;charset=euc-kr" %>
<%
for(String s: request.getParameterValues("ch")){
out.println(s+"<br>");
}
%>
http://localhost:8090/ap2/a.jsp?a=1&b=2&c=3
출력 b a c <----정렬되지 않고 출력된다.~
====================================================================================================
getParameterNames() 활용
<%@ page contentType="text/html;charset=euc-kr" %>
<%@ page import="java.util.*"%>
<%
Enumeration<String> e = request.getParameterNames();
while(e.hasMoreElements())
out.println(e.nextElement());
%>
[ex] a.jsp를 아래와 같이 실행한뒤
http://localhost:8282/ap1/a.jsp?a=11&b=66
'문서'를 클릭했을때 위url에서 입력했던 파라미터의값을
출력되게하려면 아래소스코드를 어떻게 수정해야할까?
11
66
a.jsp
=====
<%@ page contentType="text/html;charset=euc-kr" %>
<a href="b.jsp">문서</a>
b.jsp
=====
<%@ page contentType="text/html;charset=euc-kr" %>
<%=request.getParameter("a")%><br>
<%=request.getParameter("b")%><br>
[an]
a.jsp
=====
<%@ page contentType="text/html;charset=euc-kr" %>
<%
String s1=request.getParameter("a");
String s2=request.getParameter("b");
%>
<a href="b.jsp?aa=<%=s2%>&bb=<%=s2%>">문서</a>
b.jsp
=====
<%@ page contentType="text/html;charset=euc-kr" %>
<%=request.getParameter("aa")%><br>
<%=request.getParameter("bb")%><br>
====================================================================================================
[ex]
ap1/WEB-INF/src/Member.java
ap1/WEB-INF/classes
ap1/a.jsp
package a.b;
public class Member{
private String id;
public void setId(String id){
this.id=id;
}
public String getId(){
return id;
}
}
ap1/WEB-INF/classes> javac -d . ../src/Member.java
a.jsp
=====
<%@ page contentType="text/html;charset=euc-kr" %>
<%@ page import="a.b.Member" %>
<%
Member m=new Member();
out.println(m);
%>
====================================================================================================
[ex]
a.jsp
=====
<%@ page contentType="text/html;charset=euc-kr" %>
<%@ page import="java.io.*" %> //import하는 부분이다 잘 기억하도록
<%!
void a(JspWriter out) throws IOException{
out.println("aaa");
}
%>
<%
a(out);
%>
====================================================================================================
[ex]
http://localhost:8282/ap1/a.jsp
문서
a.jsp
=====
<%@ page contentType="text/html;charset=euc-kr" %>
<a href="<%=request.getContextPath()%>/b.jsp">문서</a> //getContextPath는 경로지정
404
====================================================================================================
[ex]
http://localhost:8282/ap1/a.jsp?a=11&a=22&b=33&c=44
<%@ page contentType="text/html;charset=euc-kr" %>
<%@ page import="java.util.*" %>
<%
Map<String,String[]> map=request.getParameterMap(); // map은 sort되지 않는다.
Set<String> set=map.keySet();
for(String key:set){
out.println(key+"<br>");
}
%>
keySet() : 선택한 키값을 리턴
entrySet() :이 키와 값을 모두리턴
====================================================================================================
[ex] 아래와 같이 실행될 수 있도록 소스코드를 작성하시오.
http://localhost:8282/ap1/a.jsp?a=11&a=22&b=33&c=44
a : 11, 22
b : 33
c : 44
[an]
<%@ page contentType="text/html;charset=euc-kr" %>
<%@ page import="java.util.*" %>
<%
Map<String,String[]> map=request.getParameterMap();
for(String key: map.keySet()){
out.println(key + ":");
for(String v: map.get(key)){
out.println(v+",");
}
out.println("<br>");
}
%>
[참고]
request.getParameterMap()는 request 에 담긴 parameter만 빼내서 Map으로 저장해주는 메서드이다.
====================================================================================================
# method방식 : get, post
[ex]
a.jsp
=====
<%@ page contentType="text/html;charset=euc-kr" %>
<form action="b.jsp" method="post"> // get은 한글이 깨지고 post는 한글 안깨짐.
<input type="text" name="t">
<input type="submit">
</form>
b.jsp
=====
<%@ page contentType="text/html;charset=euc-kr" %>
<% request.setCharacterEncoding("euc-kr");%> // 한글이 안깨지게 해주는 로직
<%=request.getParameter("t")%>
# 화면이동하는 세가지 방식
1. response.sendRedirect("xxx.jsp");
2. <script>
location.href="xxx.jsp";
</script>
3. <jsp:forward page="xxx.jsp" />
: 현재 request 주소와 다음 request 주소가 같다! 유일하게
====================================================================================================
[ex]
a.jsp
=====
<%@ page contentType="text/html;charset=euc-kr" %>
<form action="b.jsp">
<input type="text" name="id"><br>
<input type="text" name="pw"><br>
<input type="text" name="addr"><br>
<input type="text" name="tel"><br>
<input type="submit">
</form>
b.jsp
=====
<%@ page contentType="text/html;charset=euc-kr" %>
<jsp:forward page="c.jsp" />
c.jsp
=====
<%@ page contentType="text/html;charset=euc-kr" %>
<%=request.getParameter("id")%><br>
<%=request.getParameter("pw")%><br>
<%=request.getParameter("addr")%><br>
<%=request.getParameter("tel")%><br>
'jsp' 카테고리의 다른 글
DB연동 회원가입,삭제 (0) | 2013.10.31 |
---|---|
DB 연동 (0) | 2013.10.30 |
jsp와 db연동 (0) | 2013.10.28 |
jsp 1일차 테스트 (0) | 2013.10.22 |
JSP 첫시작~! 1일차 (0) | 2013.10.21 |