본문 바로가기
CLASS/SERVLET

#8-1 / 게시판

by eungSe__ 2024. 6. 21.

login.html 과 이어짐

-> top.jsp 에 추가 ( <%@include file="./qawrite.jsp" %>)   =>   top.jsp 아래 qawrite.jsp가 들어감

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%	
HttpSession hs = request.getSession(); //object 배열형태
String id = (String)hs.getAttribute("id");
String name = (String)hs.getAttribute("name");

if(id==null || name==null){
%>
<script>
    alert("로그인을 하셔야 1:1문의가 진행됩니다.");
    location.href = "./login.html";
</script>
<%
}
%>

<body>
    <%if (id==null || name ==null) {%> <!-- 로그인 전 -->
    <p><a href="./login.html">로그인</a>| 회원가입 | 고객센터</p>
    <% }else { %> <!-- 로그인 후 -->
    <p><%=name%>님 환영합니다. | 장바구니 | 고객센터 | <input type="button" value="로그아웃" id="btn"></p>
    <% } %>
    <%@include file="./qawrite.jsp" %>
</body>

그럼 이렇게 넣어도 됨 , id도 hidden값으로 동일하게 value 넣어놓기!

 

🔽

로그인 후 작성자를 readonly input에 넣어주게됨

 

🔽 글쓰기 input 검토 등 기능 js로 구현 

⚡ 파일첨부 input 핸들링
<td id="uploadBox">
    <div class="file_upload_sec">
        <label for="attach1"> 
	    <!--fake input-->
            <input type="text" id="file2" class="file_text" title="파일 첨부하기" readonly>
        </label>
        <div class="btn_upload_box">
            <button type="button" id="file_btn" class="btn_upload" title="찾아보기">
                <em>찾아보기</em>
            </button>
            <!--진짜 input-->
            <input type="file" id="file1" name="mfile" class="file" title="찾아보기"> 
            <span class="btn_gray_list">
                <button type="button" class="btn_gray_big"><span>+ 추가</span></button> 
            </span>
        </div>
    </div>
</td>

 

$.fn.fileck = function(){
    var $fnm = $("#file1").val();
    $("#file2").val($fnm);
}
setInterval($.fn.fileck,100);

=> chage는 모바일에서 안먹힐수도 있어 setInterval로 핸들링 해주는게 안전하다!

 

 

table명 : qa 
top.jsp => (qawrite.jsp) 해당 내용을 db에 저장되도록 합니다.
단, 첨부파일일 경우 qa_upload라는 디렉토리에 저장되도록 해야합니다.

저장 backend-파일경로 : qa_writeok.do로 post 방식으로 합니다.

 

이렇게 만듬 ㅋ

create table qa(
idx int(6) unsigned not null auto_increment,
uidx int(6) not null,
uid varchar(30) not null,
uname char(50) not null,
qa_title varchar(100) not null,
qa_pass varchar(50) not null,
qa_subject varchar(200) not null,
qa_text text not null,
qa_file text null,
qa_date timestamp not null default current_timestamp,
primary key(idx,uidx,uid),
foreign key(uidx,uid) references user (uidx,uid)
);

 

 

//너무어렵다 파일첨부 총집합...

@MultipartConfig(
	fileSizeThreshold = 1024 * 1024 * 2, 
	maxFileSize = 1024 * 1024 * 10, 
	maxRequestSize = 1024 * 1024* 100
)
public class qa_writeok extends HttpServlet {
	private static final long serialVersionUID = 1L;

	dbconfig db = new dbconfig();
	PrintWriter pw = null;
	Connection con = null;
	Statement st = null;
	PreparedStatement ps = null;
	ResultSet rs = null;

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		this.pw = response.getWriter();

		String uid = request.getParameter("mid");
		String uname = request.getParameter("qname");
		String qa_title = request.getParameter("qcategory");
		String qa_pass = request.getParameter("qpw");
		String qa_subject = request.getParameter("qsubject");
		String qa_text = request.getParameter("qcontent");

		Part qfile = request.getPart("mfile");
		String filename = qfile.getSubmittedFileName();
		long filesize = qfile.getSize();

		try {
			String filesave = "";
			if (filename == "" || filesize <= 0) {
				filesave = "";
			} else {
				String url = request.getServletContext().getRealPath("/qa_upload/");
				System.out.println(url);
				qfile.write(url + filename);

				filesave = "./qa_upload/" + filename;
			}
			this.con = this.db.info();
			
			String uidx_sql = "select * from user where uid='"+ uid +"'";
			this.ps = this.con.prepareStatement(uidx_sql);
			this.rs = this.ps.executeQuery();
			String uidx = "";
			while(this.rs.next()) {
				uidx = this.rs.getString("uidx");
			}
			
			String sql = "insert into qa(idx,uidx,uid,uname,qa_title,qa_pass,qa_subject,qa_text,qa_file,qa_date) "
					+ "values('0','"+ uidx +"','"+uid+"','"+uname+"','"+ qa_title +"','"+ qa_pass +"','"+qa_subject+"','"+qa_text+"','"+ filesave +"',now())";
			this.st = this.con.createStatement();
			int result = this.st.executeUpdate(sql);

			if(result>0) {
				this.pw.write("<script>"
						+ "alert('정상적으로 글쓰기가 완료 되었습니다');"
						+ "</script>");
			}else {
				this.pw.write("<script>"
						+ "alert('올바른 파일이 아닙니다');"
						+ "</script>");
			}

		} catch (Exception e) {
			System.out.println("db연결 실패123");
		} finally {
			try {
				this.pw.close();
				this.st.close();
				this.rs.close();
				this.ps.close();
				this.con.close();
			} catch (Exception e2) {
				System.out.println("database 접속해제 실패");
			}

		}

	}

}

'CLASS > SERVLET' 카테고리의 다른 글

#8-2 / 게시판 insert 모듈화...  (0) 2024.06.21
#7-3 / 첨부파일 기능 + database 저장  (0) 2024.06.19
#7-2 / 첨부파일 기능 - java + io  (0) 2024.06.19
#7-1 / 회원 가입 후 로그인,로그아웃  (0) 2024.06.19
#6-3 / storage  (0) 2024.06.18