본문 바로가기
JSP

230117_기본

by 경 훈 2023. 1. 17.

JSP : 기본이 HTML

<%!  %> : declation 선언부 필드명 변수 확보

<%   %> : 스크립트릿.. scriptlet..  자바코드

<%=변수%> : exprosion : 표현식 출력부분

 

폼에서 자료를 입력한 다음 전송 버튼을 누르면

입력한 자료가 넘어갈때 자동으로 객체 생성 되는데 이를

내장객체가 여러개가 생성(request, response, out)

다른 곳으로 자료를 넘기는 방법

1.링크 <a href="  "?변수=<%=변수%>>   ...html영역

2.리다이렉트 response.sendRedirect()    ...자바코드 내에서

3.forword

4.Dispatcher

 

 

<%@page import="java.net.URLEncoder"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%!
	String id;
	%>
	<%
	request.setCharacterEncoding("utf-8");
	id=request.getParameter("id");
	//response.sendRedirect("ccc.jsp?id="+id);
	%>
	<jsp:forward page="ccc.jsp">
		<jsp:param value="<%=id%>" name="id"/>
	</jsp:forward>
</body>
</html>

 

<%@page import="java.net.URLEncoder"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%!
	String id;
	%>
	<%
	request.setCharacterEncoding("utf-8");
	id=request.getParameter("id");
	RequestDispatcher rd = request.getRequestDispatcher("ccc.jsp");
	rd.forward(request, response);
	%>
	
</body>
</html>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="mid2.jsp" method="post">
		<table border="1">
			<tr>
				<th>아이디</th>
				<td><input type="text" name="id"></td>
			</tr>
			<tr>
				<th>패스워드</th>
				<td><input type="password" name="pw"></td>
			</tr>
			<tr>
				<th>나이</th>
				<td><input type="text" name="age"></td>
			</tr>
			<tr>
				<th>성별</th>
				<td><input type="radio" name="gender" value="남자">남
				<input type="radio" name="gender" value="여자">여</td>
			</tr>
			<tr>
				<th>자기소개</th>
				<td><textarea rows="10" cols="30" name="bigo"></textarea>
				</td>
			</tr>
			<tr>
				<td colspan="2" align="center"><input type="submit" value="전송">
				<input type="reset" value="초기화">
				</td>
			</tr>
		
		</table>		
	</form>
</body>
</html>

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%!

	%>
	<%
	request.setCharacterEncoding("utf-8");
	RequestDispatcher rd = request.getRequestDispatcher("out2.jsp");
	rd.forward(request, response);
	%>
</body>
</html>

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="ima.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%!
	String id;
	String pw;
	int age;
	String gender;
	String bigo;
	%>
	<%
	request.setCharacterEncoding("utf-8");
	id=request.getParameter("id");
	pw=request.getParameter("pw");
	age=Integer.parseInt(request.getParameter("age"));
	gender=request.getParameter("gender");
	bigo=request.getParameter("bigo");
	%>
	<table border="1">
		<tr>
			<th>아이디</th>
			<td><%=id%></td>
		</tr>
		<tr>
			<th>비밀번호</th>
			<td><%=pw%></td>
		</tr>
		<tr>
			<th>나이</th>
			<td><%=age%></td>
		</tr>
		<tr>
			<th>성별</th>
			<td><%=gender%></td>
		</tr>
		<tr>
			<th>자기소개</th>
			<td><%=bigo%></td>
		</tr>
		<tr>
			<td colspan="2" align="center">
			<a href="sample2.jsp">
			<input type="button" value="입력창으로"></a>
			</td>
		</tr>
	</table>
	<br>
	<jsp:include page="ima.jsp" />
</body>
</html>

package day4_jsp3;

public class ScoreDTO implements Comparable<ScoreDTO>{
	String name;
	int kor,eng,mat;
	int tot;
	public ScoreDTO() {}
	public ScoreDTO(String name, int kor, int eng, int mat, int tot) {
		super();
		this.name = name;
		this.kor = kor;
		this.eng = eng;
		this.mat = mat;
		this.tot = tot;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getKor() {
		return kor;
	}
	public void setKor(int kor) {
		this.kor = kor;
	}
	public int getEng() {
		return eng;
	}
	public void setEng(int eng) {
		this.eng = eng;
	}
	public int getMat() {
		return mat;
	}
	public void setMat(int mat) {
		this.mat = mat;
	}
	public int getTot() {
		return tot;
	}
	public void setTot(int tot) {
		this.tot = tot;
	}
	@Override
	public int compareTo(ScoreDTO o) {
		if(tot<o.tot) return 1;
		else return -1;
//		return name.compareTo(o.name);
	}
}

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
table {
	border-collapse:collapse;
	border-color: orange;
	text-align: center;
}
th,td {
	background-color: #dddddd;
	text-align: center;
}
input {
	background-color: #eeeeee;
}
</style>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="scoresave.jsp" method="post">
		<table border="2" align="center">
			<caption>회원정보입력</caption>
			<tr>
				<th>이름</th>
				<td><input type="text" name="name"></td>
			</tr>
			<tr>
				<th>국어</th>
				<td><input type="text" name="kor"></td>
			</tr>
			<tr>
				<th>영어</th>
				<td><input type="text" name="eng"></td>
			</tr>
			<tr>
				<th>수학</th>
				<td><input type="text" name="mat"></td>
			</tr>
			<tr>
				<td colspan="2" align="center">
				<input type="submit" value="전송">
				<input type="reset" value="초기화">
				</td>
			</tr>
		</table>
	</form>
</body>
</html>

 

<%@page import="java.text.DecimalFormat"%>
<%@page import="java.text.ChoiceFormat"%>
<%@page import="java.util.Collections"%>
<%@page import="day4_jsp3.ScoreDTO"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
table {
	border-collapse:collapse;
	border-color: orange;
	text-align: center;
}
th,td {
	background-color: #eeeeee;
	text-align: center;
}
span {
	color: red;
}
</style>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%!
	DecimalFormat df = new DecimalFormat("#,##0.0");
	String name;
	String bigo;
	int kor,eng,mat,tot;
	int av;
	double avg;
	String hak;
	int grade = 0;
	ArrayList<ScoreDTO> list = new ArrayList<ScoreDTO>();
	double [] a= {0,60,70,80,90};
	String [] b= {"F","D","C","B","A"};
	ChoiceFormat cf = new ChoiceFormat(a,b);
	%>
	<%
	request.setCharacterEncoding("utf-8");
	name=request.getParameter("name");
	kor=Integer.parseInt(request.getParameter("kor"));
	eng=Integer.parseInt(request.getParameter("eng"));
	mat=Integer.parseInt(request.getParameter("mat"));
	tot=kor+eng+mat;
	ScoreDTO dto = new ScoreDTO();
	list.add(new ScoreDTO(name,kor,eng,mat,tot));
	%>
	<table border="1" align="center">
		<tr>
			<th>이름</th><th>국어</th><th>영어</th><th>수학</th><th>총점</th>
			<th>평균</th><th>학점</th><th>순위</th><th>비고</th>
		</tr>
		<%
		Collections.sort(list); 
		for(ScoreDTO a:list) {
			bigo="";
			name=a.getName();
			kor=a.getKor();
			eng=a.getEng();
			mat=a.getMat();
			tot=a.getTot();
			avg=(double)tot/3;
			hak=cf.format(avg);
			grade++;
			av=(int)avg/10;
			for(int i=1;i<=av;i++) {
				bigo=bigo+"★";
			}
			%>
		<tr>
			<td><%=name%></td>
			<td><%=kor%></td>
			<td><%=eng%></td>
			<td><%=mat%></td>
			<td><%=tot%></td>
			<td><%=df.format(avg)%></td>
			<td><%=hak%></td>
			<td><%=grade%></td>
			<td><span><%=bigo%></span></td>
		<tr>
		<%
		}
		%>
	</table>
	<br>
	<div align="center">
	<a href="scoreForm.jsp"><input type="button" value="입력으로"></a>
	</div>
</body>
</html>

package day4_jsp4;

public class DTO implements Comparable<DTO>{
	String name,spname;
	int su,dan,price;
	public DTO() {}
	public DTO(String name, String spname, int su, int dan, int price) {
		super();
		this.name = name;
		this.spname = spname;
		this.su = su;
		this.dan = dan;
		this.price = price;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSpname() {
		return spname;
	}
	public void setSpname(String spname) {
		this.spname = spname;
	}
	public int getSu() {
		return su;
	}
	public void setSu(int su) {
		this.su = su;
	}
	public int getDan() {
		return dan;
	}
	public void setDan(int dan) {
		this.dan = dan;
	}
	public int getPrice() {
		return price;
	}
	public void setPrice(int price) {
		this.price = price;
	}
	@Override
	public int compareTo(DTO o) {
		if(price>o.price) return -1;
		else return 1;
	}
}

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="out.jsp">
		<table border="1" align="center">
			<tr>
				<th>회사명</th>
				<td><input type="text" list="com" name="name">
				<datalist id="com">
					<option value="삼성">삼성
					<option value="LG">LG
					<option value="SK">SK
					<option value="네이버">네이버
					<option value="카카오">카카오
				</datalist>
				</td>
			</tr>		
			<tr>
				<th>상품명</th>
				<td><input type="text" list="sp" name="spname">
				<datalist id="sp">
					<option value="이모티콘">이모티콘
					<option value="반도체">반도체
					<option value="스마트폰">스마트폰
					<option value="지도">지도
					<option value="모듈러">모듈러
				</datalist>				
				</td>	
			</tr>	
			<tr>
				<th>수량</th>
				<td><input type="text" name="su"></td>	
			</tr>	
			<tr>
				<th>단가</th>
				<td><input type="text" name="dan"></td>	
			</tr>
			<tr>
				<td colspan="2" align="center">
				<input type="submit" value="전송">
				<input type="reset" value="초기화">
				</td>	
			</tr>		
		</table>
	</form>
</body>
</html>

 

<%@page import="java.text.DecimalFormat"%>
<%@page import="java.util.Collections"%>
<%@page import="day4_jsp4.DTO"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
	tr,td {
		text-align: center;
	}

</style>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%!
	String name,spname;
	int su,dan,price;
	long count;
	ArrayList<DTO> list = new ArrayList<DTO>();
	DecimalFormat df = new DecimalFormat("#,##0");
	%>
	<%
	request.setCharacterEncoding("utf-8");
	name=request.getParameter("name");
	spname=request.getParameter("spname");
	su=Integer.parseInt(request.getParameter("su"));
	dan=Integer.parseInt(request.getParameter("dan"));
	price=su*dan;
	list.add(new DTO(name,spname,su,dan,price));
	count = list.stream().count();
	%>
	<table border="1" align="center">
		<tr>
			<th>회사명</th><th>상품명</th><th>수량</th><th>단가</th><th>금액</th>
		</tr>
		<%
		Collections.sort(list);
		for(DTO a:list) {
			name = a.getName();
			spname = a.getSpname();
			su = a.getSu();
			dan = a.getDan();
			price = a.getPrice();
			%>
		<tr>
			<td><%=name%></td>
			<td><%=spname%></td>
			<td><%=df.format(su)%></td>
			<td>₩<%=df.format(dan)%></td>
			<td>₩<%=df.format(price)%></td>	
		</tr>
		<%
		}
		int hap = list.stream().mapToInt(DTO::getPrice).sum();
		%>
	</table>
	<div align="center">
	<a href="form.jsp"><input type="button" value="입력창으로"></a>
	<br>
	데이터의 갯수:<%=count%>개
	<br>
	금액의 합은 <%out.println(df.format(hap));%>입니다.
	</div>
</body>
</html>

 

'JSP' 카테고리의 다른 글

230119_DB  (0) 2023.01.19
230118_DB  (0) 2023.01.18
230116_CSS  (1) 2023.01.16
230113_HTML  (0) 2023.01.13
230112_HTML  (1) 2023.01.12

댓글