壓縮包解壓

把sqljdbc42.jar(java8版本)或者sqljdbc4" />

国产成人精品无码青草_亚洲国产美女精品久久久久∴_欧美人与鲁交大毛片免费_国产果冻豆传媒麻婆精东

15158846557 在線咨詢 在線咨詢
15158846557 在線咨詢
所在位置: 首頁 > 營銷資訊 > 網(wǎng)站運營 > Java實戰(zhàn)Tomcat+Servlet+Sql開發(fā)簡單網(wǎng)站配置環(huán)境

Java實戰(zhàn)Tomcat+Servlet+Sql開發(fā)簡單網(wǎng)站配置環(huán)境

時間:2023-05-23 15:48:01 | 來源:網(wǎng)站運營

時間:2023-05-23 15:48:01 來源:網(wǎng)站運營

Java實戰(zhàn)Tomcat+Servlet+Sql開發(fā)簡單網(wǎng)站配置環(huán)境:

裝JDBC 配置SqlServer

1.下載Microsoft SQL Server JDBC 驅(qū)動程序

注意Java的版本,java7、8、12要安裝不同的包。

壓縮包解壓

把sqljdbc42.jar(java8版本)或者sqljdbc41.jar(java7版本)放到j(luò)ava安裝目錄下的jre/lib/ext文件夾中。

最終路徑:你的某個磁盤/java/jdk/lib/sqljdbc42.jar




2.配置SqlServer

打開SQL Server配置工具,開啟TCP/IP,將其中一個IP設(shè)置為127.0.0.1,TCP端口設(shè)置為1433,重啟SQL Server服務(wù)。然后重啟電腦。安裝步驟就這樣。剩下就是寫java代碼了。

3.寫代碼

Java加載類包這樣寫:Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

URL這樣寫:String url = "jdbc:sqlserver://127.0.0.1:1433";







二、Java使用JDBC連接SQL Server數(shù)據(jù)庫實現(xiàn)學(xué)生成績信息系統(tǒng)

1.需求任務(wù)

使用SQL Server數(shù)據(jù)庫或者MySQL數(shù)據(jù)庫各自的客戶端工具,完成如下任務(wù):

(1)創(chuàng)建數(shù)據(jù)庫students;

(2)在數(shù)據(jù)students中創(chuàng)建表scores,包括如下字段:學(xué)號、姓名、性別、得分,字段類型自行定義。學(xué)號為主鍵。 接著使用JDBC編寫Java程序,完成如下任務(wù):

(1)在表格scores插入5條記錄,代表5個學(xué)生,三位男生二位女生,各字段內(nèi)容自定(“得分”字段填入自定分數(shù));

(2)顯示5位學(xué)生的所有信息;

(3)將三位男生的得分減去5分,將兩位女生的成績加上3分;

(4)從鍵盤輸入不同學(xué)號,根據(jù)學(xué)號顯示相應(yīng)學(xué)生的所有信息。

2.窗口界面設(shè)計

3.文件目錄




4.SqlCode.java存放靜態(tài)SQL代碼

/* * 這里放的是 靜態(tài)Sql代碼*/public class SqlCode { // 在數(shù)據(jù)students中創(chuàng)建表scores static String createTable = "" + "USE students;" + "/n" + "CREATE TABLE scores" + "(" + "sno int not null," + "name varchar(20) not null," + "ssex varchar(10) CHECK(ssex IN('boy','girl'))," + "score int not null," + "PRIMARY KEY(sno)," + ")"; //在表格scores插入5條記錄 static String insertValues = "" + "USE students" + "/n" + "INSERT INTO scores(sno,name,ssex,score) VALUES(1,'DaWang','boy','61')" + "/n" + "INSERT INTO scores(sno,name,ssex,score) VALUES(2,'ErWang','girl','62')" + "/n" + "INSERT INTO scores(sno,name,ssex,score) VALUES(3,'SanWang','boy','63')" + "/n" + "INSERT INTO scores(sno,name,ssex,score) VALUES(4,'siWang','girl','65')" + "/n" + "INSERT INTO scores(sno,name,ssex,score) VALUES(5,'wuWang','girl','66')"; //顯示5位學(xué)生的所有信息 static String queryString = "" + "USE students" + "/n" + "SELECT TOP 5 * FROM scores"; //將三位男生的得分減去5 tucao:男生真累 static String updateScoreBoy = "" + "USE students" + "/n" + "UPDATE scores " + "/n" + "SET score = score - 5" + "/n" + "WHERE ssex = 'boy'" + "/n"; //將兩位女生的成績加上3分 static String updateScoreGirl = "" + "USE students" + "/n" + "UPDATE scores " + "/n" + "SET score = score + 3" + "/n" + "WHERE ssex = 'girl'" + "/n"; //刪除某個學(xué)號 自己測試數(shù)據(jù)用的 static String deleteByIdSql = "USE students" + "/n" + "DELETE FROM scores WHERE sno = ";}


5.SqlServerStu.java文件就是主文件,使用JDBC來操作數(shù)據(jù)庫

class sqlServer{ private Connection connection = null; //連接接口實例 private Statement statmment = null; //執(zhí)行靜態(tài)sql的接口實例 private PreparedStatement preStatement = null; //執(zhí)行動態(tài)sql的接口實例 private ResultSet resSet = null; // sql查詢的返回數(shù)據(jù)集合 String dbName = "students"; //數(shù)據(jù)庫名 String tbName = "scores"; //數(shù)據(jù)表名 沒必要其實 String url = "jdbc:sqlserver://127.0.0.1:1433"; //sqlserver連接地址url String userName = "sa"; //sqlserver的賬號名 要在SMSS安全性里面設(shè)置 String passWord = "root"; //sqlserver的賬號的密碼 要在SMSS安全性里面設(shè)置 //下面就是按課題要求寫的一些靜態(tài)代碼(String字符串類型,在SqlCode.java文件中的全局變量) String createTableSql = SqlCode.createTable; String insertSql = SqlCode.insertValues; String queryAllSql = SqlCode.queryString; String updateBoySql = SqlCode.updateScoreBoy; String updateGrilSql = SqlCode.updateScoreGirl; String delByIdSql = SqlCode.deleteByIdSql; //無參構(gòu)造函數(shù) 初始化建立連接 public sqlServer() { // TODO Auto-generated constructor stub try { //加載數(shù)據(jù)庫驅(qū)動 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); //DriverManager接口獲取連接 this.connection = DriverManager.getConnection(url,userName,passWord); //獲取 執(zhí)行數(shù)據(jù)庫靜態(tài)SQL語法的接口 this.statmment = connection.createStatement(); if(connection != null) { System.out.println("連接成功!"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //有參構(gòu)造函數(shù) urlParam初始化建立連接 public sqlServer(String urlParam) { // TODO Auto-generated constructor stub try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); this.connection = DriverManager.getConnection(urlParam); this.statmment = connection.createStatement(); if(connection != null) { System.out.println("連接成功!"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //關(guān)閉連接 public void close() throws SQLException { if(resSet != null) { resSet.close(); } if(statmment != null) { statmment.close(); } if(preStatement != null) { preStatement.close(); } if(connection != null) { System.out.println("關(guān)閉連接!"); connection.close(); } } //打印輸出 ResultSet集合中的數(shù)據(jù) public void rsPrint(ResultSet rS) throws SQLException { if(rS == null) return; System.out.println(""); System.out.println("sno"+"| name"+" | ssex"+" | score"); while(rS.next()) { int sno = rS.getInt("sno"); String name = rS.getString("name"); String ssex = rS.getString("ssex"); int score = rS.getInt("score"); System.out.println(sno+" | "+name+" | "+ssex+" | "+score); } } //返回ResultSet集合 public ResultSet queryBySno(int snoId) throws SQLException { String queryByIdString = "" + "USE students" + "/n" + "SELECT * FROM scores" + "/n" + "WHERE scores.sno = ?" + ""; this.preStatement = connection.prepareStatement(queryByIdString); preStatement.setInt(1, snoId); return preStatement.executeQuery(); } //查詢?nèi)?public ResultSet queryAll(String querySql) throws SQLException { return statmment.executeQuery(querySql); } //創(chuàng)建數(shù)據(jù)庫 public void generalExc(String sql) throws SQLException { preStatement = connection.prepareStatement(sql); preStatement.executeUpdate(); } //創(chuàng)建數(shù)據(jù)庫 public void createDataBase(String dbName) throws SQLException { String createSql = "CREATE DATABASE "+dbName; preStatement = connection.prepareStatement(createSql);// preStatement.setString(1, dbName); preStatement.executeUpdate(); System.out.println("創(chuàng)建數(shù)據(jù)庫"+dbName+"成功!"); } //刪除數(shù)據(jù)庫 public void delDataBase(String dbName) throws SQLException { String deleteSql = "DROP DATABASE "+dbName; preStatement = connection.prepareStatement(deleteSql);// preStatement.setString(1, dbName); preStatement.executeUpdate(); System.out.println("刪除數(shù)據(jù)庫"+dbName+"成功!"); } //通過sno學(xué)號刪除 數(shù)據(jù)表中的記錄 public void delById(int sno) throws SQLException { preStatement = connection.prepareStatement(delByIdSql + sno); preStatement.executeUpdate(); System.out.println("刪除記錄"+"成功!"); } //創(chuàng)建數(shù)據(jù)表 public void createTable(String createSql) throws SQLException { statmment.execute(createSql); System.out.println("創(chuàng)建數(shù)據(jù)表"+"成功!"); } //插入數(shù)據(jù)到數(shù)據(jù)表 public void insertValue(String insertSql) throws SQLException { statmment.execute(insertSql); System.out.println("刪除數(shù)據(jù)表"+"成功!"); } //更新數(shù)據(jù)表中的數(shù)據(jù) public void updateValue(String updateSql) throws SQLException { statmment.execute(updateSql); System.out.println("更新完成!"); } //scanner輸入指定學(xué)號,查詢學(xué)生信息 public void inputSnoAndQuery() throws SQLException { Scanner inputScanner = new Scanner(System.in); int snoId = inputScanner.nextInt(); rsPrint(queryBySno(snoId)); } //返回值:把ResultSet集合中的數(shù)據(jù)轉(zhuǎn)換成String類型 (因為后面展示到窗口文本域需要string類型) public String returnString(ResultSet rS) throws SQLException { // TODO Auto-generated method stub StringBuffer myBuffer = new StringBuffer(); int line = 0; while(rS.next()) { if(line == 0) { line++; myBuffer.append("查詢結(jié)果如下: "+"/n");// myBuffer.append("sno"+"| name"+" | ssex"+" | score"+"/n"); } int sno = rS.getInt("sno"); String name = rS.getString("name"); String ssex = rS.getString("ssex"); int score = rS.getInt("score"); myBuffer.append(sno+" | "+name+" | "+ssex+" | "+score+"/n"); } if(line == 0) myBuffer.append(""); return myBuffer.toString(); } }class window{ //組件 public JFrame sqlWindowFrame; public JPanel PanelSouth; public JPanel PanelNorth; public JTextArea textArea; public JScrollPane scrollPane; public JTextField inpuTextField; //一系列按鈕 public JButton customQueryBtn; //執(zhí)行自定義sql代碼的查詢按鈕 public JButton noResultBtn; //執(zhí)行沒有返回值的sql代碼的按鈕 比如:create insert delete 這些 public JButton createDBBtn; //創(chuàng)建數(shù)據(jù)庫按鈕 public JButton createTBBtn; //創(chuàng)建數(shù)據(jù)表按鈕 public JButton insertBtn; //添加數(shù)據(jù)按鈕 public JButton showBtn; //展示5個學(xué)生數(shù)據(jù)的按鈕 public JButton updateBtn; //更新數(shù)據(jù)的按鈕 男-5 女+3 public JButton querySnoBtn; //通過學(xué)號查詢的按鈕 public JLabel labelSouth; //底部標(biāo)簽 public JLabel labelNorth; //頂部標(biāo)簽 public sqlServer myServer; //把sqlServer作為內(nèi)部類 //窗口構(gòu)造函數(shù) 主要用來初始化組件 public window() { // TODO Auto-generated constructor stub this.sqlWindowFrame = new JFrame("by fishers _(′?`」 ∠)_"); //設(shè)置窗體 名字為notePad this.sqlWindowFrame.setLayout(new BorderLayout()); //邊界布局方式 this.sqlWindowFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //設(shè)置關(guān)閉框 this.sqlWindowFrame.setSize(800,500); this.textArea = new JTextArea(); this.scrollPane = new JScrollPane(textArea); this.inpuTextField = new JTextField(30); this.customQueryBtn = new JButton("執(zhí)行查詢"); this.noResultBtn = new JButton("執(zhí)行無返回值的sql"); this.createDBBtn = new JButton("創(chuàng)建數(shù)據(jù)庫"); this.createTBBtn = new JButton("創(chuàng)建數(shù)據(jù)表"); this.insertBtn = new JButton("添加數(shù)據(jù)"); this.showBtn = new JButton("展示數(shù)據(jù)"); this.updateBtn = new JButton("更新數(shù)據(jù)"); this.querySnoBtn = new JButton("查詢學(xué)號"); this.PanelSouth = new JPanel(); this.PanelNorth = new JPanel(); this.labelSouth = new JLabel("輸入sql語法: "); this.labelNorth = new JLabel("內(nèi)置功能區(qū): "); this.myServer = new sqlServer(); textArea.setFont(new Font("宋體",Font.PLAIN,20)); textArea.setEditable(false); //設(shè)置文本域組件不可以編輯 itemAdd(); addListen(); } //添加組件都寫在這里 public void itemAdd() { PanelSouth.add(labelSouth); PanelSouth.add(inpuTextField); PanelSouth.add(customQueryBtn); PanelSouth.add(noResultBtn); PanelSouth.add(noResultBtn); PanelNorth.add(labelNorth); PanelNorth.add(createDBBtn); PanelNorth.add(createTBBtn); PanelNorth.add(insertBtn); PanelNorth.add(showBtn); PanelNorth.add(updateBtn); PanelNorth.add(querySnoBtn); sqlWindowFrame.add(scrollPane,BorderLayout.CENTER); sqlWindowFrame.add(PanelSouth,BorderLayout.SOUTH); sqlWindowFrame.add(PanelNorth,BorderLayout.NORTH); sqlWindowFrame.setVisible(true); } //監(jiān)聽方法都寫在這里 public void addListen() { //監(jiān)聽自定義查詢按鈕 customQueryBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String textString = inpuTextField.getText(); System.out.println(textString); if(textString != null) { try {// myServer.rsPrint(myServer.queryAll(textString)); String queryAns = myServer.returnString(myServer.queryAll(textString)); System.out.println(queryAns); textArea.setText(queryAns); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } }); //監(jiān)聽沒有返回值的按鈕 noResultBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String textString = inpuTextField.getText(); System.out.println(textString); if(textString != null) { try { myServer.generalExc(textString); textArea.setText("執(zhí)行完成!"); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } }); //監(jiān)聽創(chuàng)建數(shù)據(jù)庫按鈕 createDBBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub try { myServer.createDataBase("students"); textArea.setText("創(chuàng)建數(shù)據(jù)庫完成!"); } catch (SQLException e1) { // TODO Auto-generated catch block textArea.setText("創(chuàng)建數(shù)據(jù)庫失敗,請檢查語法是否正確!或當(dāng)前連接已經(jīng)存在該數(shù)據(jù)庫!"); e1.printStackTrace(); } } }); //監(jiān)聽創(chuàng)建數(shù)據(jù)表的按鈕 createTBBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub try { myServer.createTable(myServer.createTableSql); textArea.setText("創(chuàng)建數(shù)據(jù)表完成!"); } catch (SQLException e1) { textArea.setText("創(chuàng)建數(shù)據(jù)表失敗,請檢查語法是否正確!或當(dāng)前數(shù)據(jù)庫中已經(jīng)存在該數(shù)據(jù)表!"); e1.printStackTrace(); } } }); //監(jiān)聽插入數(shù)據(jù)的按鈕 insertBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub try { myServer.insertValue(myServer.insertSql); textArea.setText("添加數(shù)據(jù)完成!"); } catch (SQLException e1) { textArea.setText("添加數(shù)據(jù)失敗,請檢查語法是否正確!或當(dāng)前數(shù)據(jù)庫中已經(jīng)存在該數(shù)據(jù)!"); e1.printStackTrace(); } } }); //監(jiān)聽展示數(shù)據(jù)的按鈕 showBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub try { String queryAns = myServer.returnString(myServer.queryAll(myServer.queryAllSql)); System.out.println(queryAns); textArea.setText(queryAns); } catch (SQLException e1) { e1.printStackTrace(); } } }); //監(jiān)聽更新數(shù)據(jù)的按鈕 updateBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub try { myServer.updateValue(myServer.updateBoySql); myServer.updateValue(myServer.updateGrilSql); textArea.setText("更新數(shù)據(jù)完成!"); } catch (SQLException e1) { // TODO Auto-generated catch block textArea.setText("更新數(shù)據(jù)失敗,請檢查語法是否正確!"); e1.printStackTrace(); } } }); //監(jiān)聽通過學(xué)號查詢數(shù)據(jù)的按鈕 querySnoBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub try { int sno = Integer.valueOf(inpuTextField.getText()); String queryAns = myServer.returnString(myServer.queryBySno(sno));// if(queryAns == " " || queryAns == null) queryAns = "未查到該學(xué)生信息";// System.out.println(queryAns); textArea.setText(queryAns); } catch (SQLException e1) { // TODO Auto-generated catch block textArea.setText("查詢失敗,請檢查語法是否正確"); e1.printStackTrace(); } } }); }}//主進程啟動public class SqlServerStu { public static void main(String []args) throws SQLException {// String urlParam = "jdbc:sqlserver://127.0.0.1:1433?user=sa&password=root"; //這個連接url好像不能用啊// sqlServer myServer = new sqlServer();// myServer.createDataBase("students"); //創(chuàng)建數(shù)據(jù)庫// myServer.createTable(myServer.createTableSql); //創(chuàng)建數(shù)據(jù)表// myServer.insertValue(myServer.insertSql); //增// myServer.rsPrint(myServer.queryAll(myServer.queryAllSql)); //查// myServer.rsPrint(myServer.queryBySno(2)); //查// myServer.updateValue(myServer.updateBoySql); //改// myServer.delById(1); //刪// myServer.rsPrint(myServer.queryAll(myServer.queryAllSql)); //查// myServer.delDataBase("students"); //刪// myServer.close(); //關(guān)閉連接// myServer.inputSnoAndQuery();// myServer.updateValue(myServer.updateBoySql);// myServer.delDataBase("students");// myServer.createDataBase("qwertest12"); window myWindow = new window(); //最后還是做成了窗口 orz }}


三、Java實戰(zhàn)Tomcat+Servlet+Sql開發(fā)簡單網(wǎng)站配置環(huán)境

課題描述:

創(chuàng)建的students數(shù)據(jù)庫和其中的scores表

使用Tomcat作為Web服務(wù)器和Servlet容器,使用SQL Server/MySQL作為數(shù)據(jù)服務(wù)器,從瀏覽器端發(fā)起對Servlet的調(diào)用,完成如下任務(wù):

(1)向scores表中插入新的記錄,表示錄入新學(xué)生信息;(注意:學(xué)號為主鍵,插入學(xué)號相同的學(xué)生要提示錯誤;如果學(xué)號由數(shù)據(jù)庫自動生成除外)

(2)批量錄入成績,即:在一個界面上列出所有學(xué)生信息,在同一個界面上成績還可以重新錄入;錄入部分或者全部學(xué)生成績后,提交,將所有成績寫入scores表。(沒有錄入的成績,可以暫定為0寫入scores表)

(3)使用的靜態(tài)html頁面和動態(tài)Servlet的個數(shù)沒有限制。




四、Eclipse編譯器下配置Tomcat8.0.36

1.eclipse安裝

只需將下載的壓縮包進行解壓,然后雙擊 eclipse.exe 文件即可。

2.下載Tomcat

查看使用的java是哪一個版本一般都是java7、8、12;所以安裝Tomcat8.5以上即可。

下載好解壓到隨便一個目錄,我的是下面這個目錄:D:/tomcatresource




五、Eclipse環(huán)境下配置Tomcat8.0.36

1.打開Eclipse,單擊“Window”菜單,選擇下方的“Preferences”。

2.單擊“Server”選項,選擇下方的“Runtime Environments”。

3.點擊“Add”添加Tomcat。

4.點擊“Next”,選中自己安裝的Tomcat路徑

5.點擊“Finish”完成。

六、Eclipse環(huán)境下配置Tomcat8.5.38

1.打開Eclipse,單擊“Window”菜單,選擇下方的“Preferences”。

2.單擊“Server”選項,選擇下方的“Runtime Environments”。

3.點擊“Add”添加Tomcat。

4.點擊“Next”,選中自己安裝的Tomcat路徑。

5.報錯處理

從上圖可以發(fā)現(xiàn),在Eclipse集成Tomcat時,遇到了一個報錯:

The Apache Tomcat installation at this directory is version 8.5.38. A Tomcat 8.0 installation is expected.


這里我的Tomcat的版本是8.5.38,報這個錯的原因是Eclipse里面限制Tomcat的最高版本是8.0的,我用的Tomcat的版本明顯高于Eclipse的要求。具體的改法如下:

首先找到Tomcat的本地安裝路徑,然后找到lib文件夾中的catalina.jar包,用解壓軟件打開這個jar包,依次找到并且雙擊打開catalina.jar/org/apache/catalina/util/ServerInfo.properties文件,如下所示:




將文件中server.info=Apache Tomcat/8.5.38中的8.5.38改成8.0.0即可。

修改完成后重新配置Tomcat就不會報錯了,如下圖所示。

6.點擊“Finish”完成。

七、 建立一個Web應(yīng)用

1.File → New → Dynamic Web Project

2.創(chuàng)建一個Dynamic Web Project

3.點擊“Next”下一步

4.點擊“Next”下一步

5.點擊“Finish”完成




八、讓Tomcat服務(wù)器顯示在控制臺上,將Web應(yīng)用部署到Tomcat中

1.Window → Show View → Servers

2.選擇Tomcat版本

點擊鏈接No servers are available. Click ths link to create a new server,在彈出的對話框中選擇Tomcat版本。

3.點擊“Next”,添加我們的項目,選中項目并點擊Add,或是雙擊都可以添加到右邊

4.點擊“Finish”完成




返回下方的“Servers”面板,右鍵單擊該面板中的“Tomcat v8.0 Server at localhost”節(jié)點,在彈出的快捷菜單中單擊“Start”,即可啟動指定的Web服務(wù)器。

如果此時直接啟動訪問http://localhost:8080/day05 ,會發(fā)現(xiàn)會報404的錯誤。這是因為我們沒有添加主頁,下面添加主頁(index.jsp)的內(nèi)容:

<%@ 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>歡迎頁面</title></head><body> 歡迎使用eclipse部署Tomcat。</body></html>


5.web資源一定要在WebRoot目錄下添加

6.訪問鏈接







7.404錯誤碼

瀏覽器可能會訪問你的網(wǎng)頁可能出現(xiàn),解決方法:修改瀏覽器訪問的url地址

瀏覽器上訪問你的jsp頁面的url地址是這樣寫的:

8.網(wǎng)頁顯示中文亂碼

如果網(wǎng)頁顯示中文亂碼按這樣修改jsp頁面,方框里的編碼格式改成utf-8

九、新建Servlet

上面步驟做完,已經(jīng)能夠訪問一個JSP靜態(tài)頁面了。
接下來是新建Servlet,來完成動態(tài)數(shù)據(jù)展示。

1.右鍵你的項目包,新建,選擇servlet。

2.輸入名字后,直接點擊Finish,自動生成了servlet文件

3.Servlet,其實只需要重寫doGet方法,把想要的數(shù)據(jù)加入到response對象中

4.添加文本內(nèi)容

比如我append了這么一句話,"My First Hello world page2333"

4.啟動servlet,打開瀏覽器,通過url訪問你的servlet

5.出現(xiàn)404報錯

6.只需要把url地址鏈接改成http://localhost:8080/項目名/selvert名/




十、把Servlet和jsp頁面相關(guān)聯(lián)

通過Servlet可以返回你想要的數(shù)據(jù)到Response對象中;
所以只需要在doGet中,調(diào)用JDBC連接數(shù)據(jù)庫,把獲取到數(shù)據(jù)庫的數(shù)據(jù),append加入到response對象中就可以了。
然后應(yīng)該還要把response對象中的數(shù)據(jù)展示到JSP頁面中。

如何把Servlet與JSP頁面連接起來呢?也就是如何把response對象中的數(shù)據(jù)展示到JSP頁面中?

比如我想把自己寫的Servlet中的,doGet函數(shù)獲得的數(shù)據(jù),返回到JSP靜態(tài)頁面,交給自己編寫的學(xué)生信息展示JSP靜態(tài)頁面展示數(shù)據(jù),該怎么做?

1.需要先用request.setAttribute設(shè)置要傳遞的數(shù)據(jù),

2.然后在Servlet使用RequestDispatcher接口來傳遞數(shù)據(jù)了,RequestDispatcher可以把響應(yīng)工作(如展示學(xué)生信息)派發(fā)給jsp頁面、其它Servlet、或者Html頁面等。




十一、一個簡單的測試瀏覽器訪問JSP頁面

1.實現(xiàn)傳遞jsp頁面

servlet把一個學(xué)生的數(shù)據(jù)從數(shù)據(jù)庫取出,向jsp頁面?zhèn)鬟f學(xué)生數(shù)據(jù),jsp頁面展示該學(xué)生信息

在selvert的doGet函數(shù)中請求students數(shù)據(jù)庫,轉(zhuǎn)發(fā)數(shù)據(jù)到j(luò)sp頁面,jsp頁面展示數(shù)據(jù)。

測試代碼的 整個目錄結(jié)構(gòu)是這樣的,




2.selvlet中的doGet函數(shù)

其中World.java,這個selvlet中的doGet函數(shù),是這樣寫的

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); Connection connection = null; //連接接口實例 Statement statmment = null; //執(zhí)行靜態(tài)sql的接口實例 PreparedStatement preStatement = null; //執(zhí)行動態(tài)sql的接口實例 ResultSet resultSet = null; //sql查詢的返回數(shù)據(jù)集合 String userName = "sa"; //數(shù)據(jù)庫賬號 String passWord = "root"; //數(shù)據(jù)庫密碼 String url = "jdbc:sqlserver://127.0.0.1:1433"; //sqlserver連接地址url int sno = 0; String name = null; String ssex = null; int score = 0; //加載數(shù)據(jù)庫驅(qū)動 try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } //DriverManager接口獲取連接 try { connection = DriverManager.getConnection(url,userName,passWord); //獲取 執(zhí)行數(shù)據(jù)庫靜態(tài)SQL語法的接口 statmment = connection.createStatement(); if(connection != null) { System.out.println("連接成功!"); } //從數(shù)據(jù)庫中 獲取一位同學(xué)的個人信息 String querySqlString = "" + "USE students" + "/n" + "SELECT TOP 1* FROM scores"; resultSet = statmment.executeQuery(querySqlString); while(resultSet.next()) { sno = resultSet.getInt("sno"); name = resultSet.getString("name"); ssex = resultSet.getString("ssex"); score = resultSet.getInt("score"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } //用setAttribute設(shè)置傳遞的數(shù)據(jù)參數(shù) request.setAttribute ( "sno", sno) ; request.setAttribute ( "name", name) ; request.setAttribute ( "ssex", ssex) ; request.setAttribute ( "score", score) ; //重要的是getRequestDispatcher 把數(shù)據(jù)交給jsp頁面響應(yīng) request.getRequestDispatcher ( "NewFile.jsp").forward( request , response );//轉(zhuǎn)發(fā)到NewFile.jsp,讓他去具體響應(yīng) }

3.NewFile.jsp靜態(tài)頁面

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><!DOCTYPE html><html><head><meta charset="utf-8"><title>Insert title here</title></head><body>用戶學(xué)號:${sno} 用戶姓名:${name} 用戶性別:${ssex} 用戶分數(shù):${score}</body></html>

4.瀏覽器訪問JSP頁面










Java實戰(zhàn)|Tomcat+Servlet+Sql開發(fā)簡單網(wǎng)站,從配置環(huán)境開始 - fishers - 博客園 (cnblogs.com)

Java使用JDBC連接SQL Server數(shù)據(jù)庫 - fishers - 博客園 (cnblogs.com)

Java使用JDBC連接SQL Server數(shù)據(jù)庫|實現(xiàn)學(xué)生成績信息系統(tǒng) - fishers - 博客園 (cnblogs.com)

Eclipse環(huán)境下如何配置Tomcat,并且把項目部署到Tomcat服務(wù)器上_李阿昀的博客-CSDN博客_eclipse配置tomcat

【Servlet】關(guān)于RequestDispatcher的原理 - lulipro - 博客園 (cnblogs.com)

關(guān)鍵詞:簡單,配置,環(huán)境,實戰(zhàn)

74
73
25
news

版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。

為了最佳展示效果,本站不支持IE9及以下版本的瀏覽器,建議您使用谷歌Chrome瀏覽器。 點擊下載Chrome瀏覽器
關(guān)閉