Jakarta Server Pages (used to be Java Server page)
A server-side script language that inserts Java code within HTML script and creates a static web app on the server and uploads into the browser.
other sever-side lang: PHP(Hypertext Preprocessor) and ASP (Active Server Pages)
JSP work process
1.) when called, transitions to Java Servlet
2.) compiled in the server
3.) Deploys the outcome to the user (html format)
Then what's a servlet?
A server-side program that is used when creating a static web page based on Java. Converts html -> String
Creating a page from Servlet:
1.) create a Servlet

Dynamic web project > Java resources>src.main.java:right click> servlet > write package, name> name and link should match: both in lowercase/capitalize> next for more option> finish
2.) create code based on the format it needs to be in to transfer data to the server
//이게 서블릿 = java code 속에 html/css/js = String
@WebServlet("/main")
public class Main extends HttpServlet {
private static final long serialVersionUID = 1L; (long)
what the fuck is serialVersionUID???
- serialVersionUID is a unique identifier for Serializable classes
- if not explicitly declared, Java will generate one automatically => lead to unexpected issues when changes made
- best practice to explicitly declare: serialVersionUID = 1L;
When creating a Servlet, it often uses the 'serializable' interface or extends classes that are serializable like the one above.
Serializing happens when we are transmitting data to the server about our code.
When coding internally, the computer finds and retrives data that is stored in objects, variables etc via the handy address system which the values are stored in.
However the server has no access to the same values !! So we send a serialized version of our code (a recipe book) if you will for the server to have all the info it needs to compile the code we sent from their side.
We only do this for Servlets because for JSP, apache Tomcat does this for us!!
public Main() { //constructor
super(); //inheriting everything from parent
}
//uses either get or post to call server
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //do get = get
System.out.println("Get방식의 호출이 들어왔습니다");
}
//post
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8"); converts index.jsp value to UTF: allows lang other than english to be legible
System.out.println("post 방식의 호출이 들어왔습니다");
System.out.println("id :" + request.getParameter("id"));
System.out.println("pw :" + request.getParameter("pw"));
}
}
- HttpServletRequest request: a method that passes the http request information to the servlet and reads the header, parameter, cookie and url info
- HttpServletResponse: used to send info to client
- "id :" + request.getParameter("id"): the value of name = "id" inside form of index.jsp using method getParameter
PrintWriter pw = response.getWriter(); sends character text through outputsream towards client
pw.println("<DOCTYPE html>");
pw.println("<html lang=\"ko\">");
pw.println("<head>");
pw.println("<meta charset = \"UTF-8\">");
pw.println("<title>서블릿을 사용해봄</title>");
pw.println("</head>");
pw.println("<body>");
pw.println("<h1>당신이 입력한 id는" + id +"입니다</h1>");
pw.println("<h1>당신이 입력한 pw는"+ passwd +"입니다</h1>" );
pw.println("</body>");
pw.println("</html>");
pw.close();
In Conclusion: It is easier to use JSP as it doesn't need to be converted from String to HTML like above
'JSP' 카테고리의 다른 글
JSP: getting data, using prepared Statement and Singleton method (0) | 2024.07.18 |
---|