簡單java web實現介面開發

來源:互聯網
上載者:User

標籤:server   步驟   http   cte   配置   inf   需要   next   request   

有關javaweb的一個簡單的登陸介面開發 這裡使用的工具是eclipse、sql 2016、tomcat8

開發前需要在eclipse中完成tomcat和SQL的串連配置,這裡tomcat在web項目運行時會自動的啟動,下邊介紹開發步驟

一、web項目的建立

開啟eclipse點解File->New—>Dynamic Web Project

 進入以下介面,輸入項目名稱

 

點擊next

再點擊next,進入下一介面

將箭頭指向的位置選中,點擊finish及完成Web項目的建立

 

三、資料庫建表

開啟資料庫右擊資料庫建立資料庫

點擊建立好的資料庫,右擊表->建立->表

 

 

 

 

 

在這裡輸入列名,選擇好資料類型

開啟後輸入資訊

儲存及完成資料庫表的建立

四、資料庫連接

將sqljdbc4.jar檔案拖入WEB-INF中

 

五、添加JSP檔案

右擊WebContent

Finish完成JSP檔案建立

建立完成後輸入以下代碼

<%@ 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=ISO-8859-1"><title>Insert title here</title></head> <body><center><form action="check.jsp" method="post">//執行check.jsp檔案
使用者:<input type="text" name = "username"><br>密碼:<input type="password" name="pass"><br><input type="submit" value="登陸"></form></center></body></html>

運行,可完成介面的建立

輸入一下代碼

<%@ page language="java" import="java.sql.*" 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>    <%      String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";      String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=hh";      String userName="sa";//資料庫的登陸帳號密碼      String userPwd="g15034014195";    Class.forName(driverName);    Connection dbConn=DriverManager.getConnection(dbURL,userName,userPwd);    String sql = "select * from log where [user]=? and [pass]=?";//user pass代表資料庫中所建立的列名
PreparedStatement pstmt = dbConn.prepareStatement(sql); request.setCharacterEncoding("UTF-8");
String user = request.getParameter("username");
String pass = request.getParameter("pass"); pstmt.setString(1, user);
pstmt.setString(2, pass);
ResultSet rs = pstmt.executeQuery();
if(rs.next()) { %><center><h1>登陸成功!</h1></center><% } else { %><center><h1>登陸失敗!</h1></center><% } %> </body> </html>

運行結果如下

 

簡單java web實現介面開發

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.