Presentation is loading. Please wait.

Presentation is loading. Please wait.

Servlets. A form The HTML source Chapter 1 Please enter your name and password then press start Name: Password: In Netbeans you can graphically create.

Similar presentations


Presentation on theme: "Servlets. A form The HTML source Chapter 1 Please enter your name and password then press start Name: Password: In Netbeans you can graphically create."— Presentation transcript:

1 Servlets

2 A form

3 The HTML source Chapter 1 Please enter your name and password then press start Name: Password: In Netbeans you can graphically create forms and elements by using the HTML palette (Ctrl-Shift-8) Carefully set the action http address. WebApplication1 is the name of the project. GetDemo is the name of the servlet.

4 A servlet import java.io.*; import java.net.*; import javax.servlet.*; import javax.servlet.http.*; public class GetDemo extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String username = request.getParameter("uname"); String password = request.getParameter("userpw");

5 out.println(" "); out.println(" GetDemo Output "); out.println(" "); out.println("Hello " + username + " "); out.println("Your password was: " + password + " "); out.println(" "); out.close(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } public String getServletInfo() { return "Short description"; } }

6 Right click and create HTML pages with forms. Then add a link to those pages in the index.jsp page. To run press F6. This will open a browser showing a list of your HTML files. Select the one you want to display. If you have made some change to the HTML file, don ’ t forget to press refresh in your browser.

7 Connection manager import java.sql.*; import java.util.*; public class ConnectionManager { private static ConnectionManager instance = null; private Stack connections; private ConnectionManager () { connections = new Stack(); try { DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver()); } catch (Exception ex) { System.out.println(ex); } } public static ConnectionManager getInstance() { if (instance == null) instance = new ConnectionManager(); return instance; }

8 Connection manager public Connection getConnection() { Connection conn = null; if (!connections.empty()) conn = (Connection) connections.pop(); else { //No one left in the stack, create a new one try { conn = DriverManager.getConnection ("jdbc:oracle:thin:@localhost:1521:TEACH", “username", “passwd"); } catch (SQLException ex) { System.out.println("SQLException: " + ex); } return conn; } public void returnConnection(Connection conn) { if (conn != null) connections.push(conn); }

9 Insert Servlet import java.io.*; import java.net.*; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; public class Insert extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String title = request.getParameter("title"); String year = request.getParameter("year"); String length = request.getParameter("length"); String studioName = request.getParameter("studio");

10 Insert Servlet String statementString = "INSERT INTO Movie(title, year, length, studioName) " + "VALUES( '" + title + "'," + year + "," + length + ",'" + studioName + "')"; Connection conn = ConnectionManager.getInstance().getConnection(); try { Statement stmt = conn.createStatement(); stmt.executeUpdate(statementString); stmt.close(); } catch(SQLException e) { out.println(e); } ConnectionManager.getInstance().returnConnection(conn); …

11 The HTML source Insert Please enter your name and password then press start Title: Year: Length: Studio:


Download ppt "Servlets. A form The HTML source Chapter 1 Please enter your name and password then press start Name: Password: In Netbeans you can graphically create."

Similar presentations


Ads by Google