1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<%@ page language="java" contentType="text/html; charset=euc-kr"
    pageEncoding="euc-kr"%>
 
<%-- 선언문의 앞에는 !가 붙어야 한다.--%> 
<%!
public int sum() {
    int total=0;
    for(int i=1 ; i<=100 ; i++) {
        total+=i;
    }
    return total;
}
%>
<%
String str = "1부터 100까지의 합 " ;
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
 
<%=str %>은<%=sum() %>입니다.<br>
<%=str %>에 3을 곱하면<%=sum()*3 %>입니다.<br>
<%=str %>을 100으로 나누면<%=sum()/1000. %>가 됩니다.<br>
 
</body>
</html>


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<%@ page language="java" contentType="text/html; euc-kr"
    pageEncoding="euc-kr"%>
<%@ page import = "java.util.Calendar" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
현재 시간은 <%=hour %>시 <%=minute %>분 <%=second %>초 입니다.
</body>
</html>


1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>회원가입 예제</title>
</head>
<frameset cols="30%, *">
    <frame src="menu.jsp" name="leftFrame">
    <frame src="login.jsp" name="rightFrame">
</frameset>
<body>
 
</body>
</html>
index.html 파일 : 프레임 셋을 나누는것밖에 의미는 없다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="login" method="post">
    아이디 : <input type="text" name="id"/><br>
    패스워드 : <input type ="password" name="passwd"/><br>
    <input type="submit" value="로그인"/>
</form>
 
</body>
</html>

login.jsp파일 액션 태그에 login 클래스를 서블릿에서 불러올수 있또록 그리고 포스트로 지정


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<%
    String id=(String)session.getAttribute("id");
%>
<body>
<%
    if(id == null) {
%>
<a href="login.jsp" target="rightFrame"/>로그인</a>
<%
    }
    else {
%>
    <%=id %>님 환영합니다.
<%
    }
%>
 
</body>
</html>

menu.jsp파일 선택문으로 성공여부를 알려주는 코드로 작성


1
2
3
4
5
6
7
8
9
10
11
12
13
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<script>top.leftFrame.location.href="menu.jsp"</script>
<body>
로그인 성공
</body>
</html>


loginSuccess.jsp파일 성공했을 시 나타내주는 파일


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
 
 
import java.io.IOException;
import java.io.PrintWriter;
 
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
/**
 * Servlet implementation class LoginSevlet
 */
@WebServlet(name = "login", urlPatterns = { "/login" })
public class LoginSevlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginSevlet() {
        super();
        // TODO Auto-generated constructor stub
    }
 
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        request.setCharacterEncoding("euc-kr");
        response.setContentType("text/html;charset=euc-kr");
        PrintWriter out = response.getWriter();
        
        String id = request.getParameter("id");
        String passwd = request.getParameter("passwd");
        if(id.equals("baduki77")  && passwd.equals("1234s")) {
            HttpSession session = request.getSession();
            session.setAttribute("id", id);
            RequestDispatcher dispatcher = request.getRequestDispatcher("loginSuccess.jsp");
            dispatcher.forward(request, response);
        } else {
            out.println("<script>");
            out.println("alert('아이디나 비밀번호가 일치하지 않습니다.')");
            out.println("history.back()");
            out.println("</script>");
        }
    }
 
}
 

LoginServlet.java : 클래스 이름이 login인것을 확실히 확인하고 선택문을 잘 확인 하기

1
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" 
URIEncoding="euc-kr"/>


마지막 URIEncoding부분을 euc-kr로 추가하면 됨..


doGet방식으로 전달할 경우 

request.setCharacterEncoding("euc-kr"); 을 서블릿 소스로 추가하면 한글로 변환할수 있음

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Post방식으로 전송법</title>
</head>
<body>
<h1>회원가입</h1>
<form action="memReg" method="post">
    회원명 : <input type="text" name="name"><br>
    주소 : <input type="text" name="addr"><br>
    전화번호 : <input type="text" name="tel"><br>
    취미 : <input type="text" name="hobby"><br>
    <input type="submit" value="확인">
</form>
</body>
</html>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 
 
import java.io.IOException;
import java.io.PrintWriter;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
/**
 * Servlet implementation class MemRegServlet
 */
@WebServlet(name = "memReg", urlPatterns = { "/memReg" })
public class MemRegServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MemRegServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
 
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setContentType("text/html;charset=euc-kr");
        PrintWriter out = response.getWriter();
        String name = request.getParameter("name");
        String addr = request.getParameter("addr");
        String tel = request.getParameter("tel");
        String hobby = request.getParameter("hobby");
        out.println("이름" + name + "<br>");
        out.println("주소" + addr + "<br>");
        out.println("전화번호" + tel + "<br>");
        out.println("취미" + hobby + "<br>");
    }
 
}
 



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>겟메소드로 정보 받아오</title>
</head>
<body>
<h1>로그인</h1>
<form action="login" method="get">
    아이디 : <input type="text" name="id" /><br>
    비밀번호 : <input type ="text" name="passwd" /><br>
    <input type="submit" value="로그인"/>    
</form>
</body>
</html>
 


HTML파일에서 로그인으로 액션값을 넘겨주고 메소드는 겟방식으로 넘겨줌.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.io.IOException;
import java.io.PrintWriter;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
/**
 * Servlet implementation class LoginServlet
 */
@WebServlet(name = "login", urlPatterns = { "/login" })
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
 
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String id = request.getParameter("id");
        String passwd = request.getParameter("passwd");
        response.setContentType("text/html;charset=euc-kr");
        PrintWriter out = response.getWriter();
        out.println("아이디 = " + id + "<br>");
        out.println("패스워드 = " + passwd + "<br>");
    }
 
}
 

서블릿 페이지에서 doGet방식으로 정보를 받아서 프린트 해주는 서블릿 페이지 테스트


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#include <stdio.h>
#include <conio.h>
#include <Windows.h>
#include "color.h"
 
int g_nEngine;
int spd = 0;
 
typedef struct _OPTION_
{
    int nHeadLight;
    int nCDPlayer;
    int nDriverUp;
    int nSunLoop;
    int nSpeedUp;
    int nSpeedDown;
    int nBreak;
} OPTION;
 
void gotoxy(int x, int y)
{
    COORD CursorPosition = {x, y};
    SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), CursorPosition);
}
 
// ─│┌ ┐┘└ ├ ┬ ┤ ┤ ◎ ■
 
void CarPrint()
{
    gotoxy(0, 4);
    printf("                        ──\n");
    //    printf("              ───────\n");
    printf("            □              □\n");
    printf("          □                  □\n");
    printf("        □□        □        □    ■\n");
    printf("      □□□□□□□□□□□□□□□■\n");
    printf("    ■□□■■■□□□□■■■□□□□\n");
    printf("    ■□□■■■□□□□■■■□□□□\n");
    printf("          ■■■        ■■■\n");
}
 
void EngineOn()
{
    setColor(RED);
    gotoxy(39, 9);
    printf("=3");
    gotoxy(39, 10);
    printf("=3");
    setColor(WHITE);
}
 
void DriverUp()
{
    setColor(GREEN);
    gotoxy(16, 6);
    printf("@\n");
    gotoxy(13, 7);
    printf("─/  \\n");
    setColor(WHITE);
}
 
void SunLoop()
{
    setColor(WHITE);
    gotoxy(13, 4);
    printf("───────\n");
    setColor(WHITE);
}
 
void HeadLightOn()
{
    setColor(YELLOW);
 
    gotoxy(0, 8);    
    printf("▒\n");
    printf("▒▒●\n");
    printf("▒▒●\n");
    printf("▒\n");
        
    setColor(WHITE);
}
 
void CDPlayerOn()
{
    setColor(LIGHTRED);
    gotoxy(17, 3);
    printf("♬♪~~~");
    setColor(WHITE);
}
 
void Speed() 
{
    if(spd > 0 && spd <50)
    {
        setColor(LIGHTGREEN);
        gotoxy(1,1);
        printf("───");
        setColor(WHITE);
        setColor(LIGHTGREEN);
        gotoxy(3,5);
        printf("─");
        setColor(WHITE);
        setColor(LIGHTGREEN);
        gotoxy(30,3);
        printf("─────");
        setColor(WHITE);
        setColor(LIGHTGREEN);
        gotoxy(40,6);
        printf("─");
        setColor(WHITE);
        setColor(LIGHTGREEN);
        gotoxy(20,2);
        printf("─");
        setColor(WHITE);
    }
    else if(spd >= 50 && spd <100)
    {
        setColor(LIGHTBLUE);
        gotoxy(1,1);
        printf("〓〓〓〓");
        setColor(WHITE);
        setColor(LIGHTBLUE);
        gotoxy(3,5);
        printf("〓");
        setColor(WHITE);
        setColor(LIGHTBLUE);
        gotoxy(30,3);
        printf("〓〓〓〓〓");
        setColor(WHITE);
        setColor(LIGHTBLUE);
        gotoxy(40,6);
        printf("〓");
        setColor(WHITE);
        setColor(LIGHTBLUE);
        gotoxy(20,2);
        printf("〓");
        setColor(WHITE);
    }
    else if(spd >= 100)
    {
        setColor(LIGHTGREEN);
        gotoxy(1,1);
        printf("〓─〓─");
        setColor(WHITE);
        setColor(LIGHTBLUE);
        gotoxy(3,5);
        printf("〓");
        setColor(WHITE);
        setColor(LIGHTGREEN);
        gotoxy(30,3);
        printf("─〓─〓─");
        setColor(WHITE);
        setColor(LIGHTBLUE);
        gotoxy(40,6);
        printf("〓");
        setColor(WHITE);
        setColor(LIGHTGREEN);
        gotoxy(20,2);
        printf("─");
        setColor(WHITE);
    }
    else
    {
        setColor(LIGHTGRAY);
        gotoxy(1,1);
        printf(" ");
        setColor(WHITE);
    }
}
 
void Break()
{
    spd = 0;
}
 
 
void CarStatusPrint (int nHeadLight, int nCDPlayer, int nDriverUp, int nSunLoop, int nSpeedUp, int nSpeedDown, int nBreak)
{
    gotoxy(50, 4);
    printf("[오픈카 상태 상태]");
 
    gotoxy(50, 6);
    printf("엔진 시동(1) :");
    if(g_nEngine)
        printf("On");
    else
        printf("Off");
 
    gotoxy(50, 7);
    printf("운전자 탑승(2) : ");
    if(nDriverUp)
        printf("In");
    else
        printf("Out");
    
    gotoxy(50, 8);
    printf("헤드라이트(3) : ");
    if(nHeadLight)
        printf("On");
    else
        printf("Off");
 
    gotoxy(50, 9);
    printf("CD 플레이어(4) : ");
    if(nCDPlayer)
        printf("On");
    else
        printf("Off");
 
    gotoxy(50, 10);
    printf("썬루프(5) : ");
    if(nSunLoop)
        printf("On");
    else
        printf("Off");
 
    gotoxy(50, 12);
    printf("주행모드(↑,↓) : ");
    if(spd > 0)
        printf("%d km", spd);
    else
        printf("0 km");
 
    gotoxy(50, 13);
    printf("브레이크 작동(0) ");
    if(nBreak)
        printf("On");
    else
        printf("Off");
 
    gotoxy(5, 14);
    printf("프로그램 종료는 q키 입력");
}
 
int main(void)
{
    OPTION sOption;
    int nKey;
 
    sOption.nHeadLight = 0;
    sOption.nCDPlayer = 0;
    sOption.nDriverUp = 0;
    sOption.nSunLoop = 0;
    sOption.nSpeedUp = 0;
    sOption.nSpeedDown = 0;
    sOption.nBreak = 0;
 
    CarPrint();
    CarStatusPrint(sOption.nHeadLight, sOption.nCDPlayer, sOption.nDriverUp, sOption.nSunLoop, sOption.nSpeedUp, sOption.nSpeedDown, sOption.nBreak);
 
    while(1)
    {
        if(_kbhit())
        {
            nKey = _getch();
 
            if(nKey == 'q')
                break;
 
            switch(nKey)
            {
            case '1' :
                if(spd == 0)
                {                
                    g_nEngine = !g_nEngine;
                }
                
                if(!g_nEngine)
                {
                    sOption.nHeadLight = 0;
                    sOption.nCDPlayer = 0;
                    sOption.nDriverUp = 0;
                }                
                break;
            case '2' :
                if(g_nEngine)
                    sOption.nDriverUp = !sOption.nDriverUp;
                break;
            case '3' :
                if(g_nEngine && sOption.nDriverUp)
                    sOption.nHeadLight = !sOption.nHeadLight;
                break;
            case '4' :
                if(g_nEngine && sOption.nDriverUp)
                    sOption.nCDPlayer = !sOption.nCDPlayer;
                break;
            case '5' :
                if(g_nEngine && sOption.nDriverUp)
                    sOption.nSunLoop = !sOption.nSunLoop;
                break;
            case 72 :
                if(g_nEngine && sOption.nDriverUp)
                {
                    spd += 5;
                    if(spd == 0)
                        sOption.nSpeedUp = 0;
                    else
                        sOption.nSpeedUp = 1;
                }
                break;
            case 80 :
                if(g_nEngine && sOption.nDriverUp)
                {
                    spd -= 5;
                    if(spd == 0 || spd <=0)
                    {
                        sOption.nSpeedUp = 0;
                        spd = 0;
                    }
                    else
                        sOption.nSpeedUp = 1;
                }
                break;
            case '0' :
                if(g_nEngine && sOption.nDriverUp)
                {
                    sOption.nSpeedUp = 0;
                    sOption.nBreak = !sOption.nBreak;
                }
                break;
            }
 
            system ("cls");
 
            CarPrint();
            if(g_nEngine)
                EngineOn();
            if(sOption.nHeadLight)
                HeadLightOn();
            if(sOption.nCDPlayer)
                CDPlayerOn();
            if(sOption.nDriverUp)
                DriverUp();
            if(sOption.nSunLoop)
                SunLoop();
            if(sOption.nSpeedUp)
                Speed();
            if(sOption.nSpeedDown)
                Speed();
            if(sOption.nBreak)
                Break();
            CarStatusPrint(sOption.nHeadLight, sOption.nCDPlayer, sOption.nDriverUp, sOption.nSunLoop, sOption.nSpeedUp, sOption.nSpeedDown, sOption.nBreak);
        }
    }
 
    return 0;
}

 

 

 

 

 

 

+ Recent posts