Java web:修改Tomcat服務(wù)器端口號(hào)的方法,jsp,集合,表格,拼接,循環(huán)【詩書畫唱】
自己總結(jié)的個(gè)人的理解和技巧:
jsp就是用<%? %>等在jsp文件中可以同時(shí)寫Java和HTML等的代碼,<%? %>里面的可以都為Java部分等,而外面的可以為HTML部分的。只要是自己認(rèn)為應(yīng)該為Java部分的代碼等都用<% %>括起來。有時(shí)要把Java中換行的語句等,轉(zhuǎn)換成HTML中的<br/>等,HTML和Java等的代碼混合起來等。——詩書畫唱

//下面是JSP常用的方法:
? ? //方法1:拼接html代碼(個(gè)人理解:用了<%=? %>,
? ?// <%? %>和HTML等的)
? ? //方法2:混搭(個(gè)人理解:
? ? //用<%? %>和HTML等的)

下面是常見的防亂碼等的JSP代碼模板(一般來說把默認(rèn)生成的jsp模板等中的字符集設(shè)置為UTF-8也可能會(huì)不容易出現(xiàn)亂碼等):
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<h1>詩書畫唱</h1>
<%
String[] name = { "百度", "京東", "B站" };
String[] hrefName = { "http://www.baidu.com", "http://www.jd.com",
for (int i = 0; i < name.length; i++) {
String html = "<a href='" + hrefName[i] + "'>" + name[i]
+ "</a>";
%>
<%=html%>
<%
}
%>
</body>
</html>? ?


下面是建jsp代碼必要的(至于怎么建可以看我發(fā)的教程視頻或?qū)诘龋?/span>


1、在jsp頁面顯示一個(gè)九九乘法表。

<%@ page language="java" contentType=
"text/html; charset=ISO-8859-1"
? ? pageEncoding="ISO-8859-1"%>
<!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>
<%
for(int j=1;j<=9;j++) {
for(int i=1;i<=j;i++){
%>
<%=j%>*<%=i%>=<%=i*j%>;
<%
}
%>
<br/>
<%
}
%>
</body>
</html>


2、創(chuàng)建一個(gè)商品類,包含名稱,價(jià)格和類型三個(gè)屬性,通過兩種循環(huán)方式拼接table在頁面顯示所有商品的信息。
插入單條的表格:


package com.SSHC;
public class Product {
? ? private String name;
? ? private Double price;
? ? private String type;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}



<%@page import="com.SSHC.Product"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html;
?charset=UTF-8" pageEncoding="UTF-8"%>
<%
? ? String path = request.getContextPath();
? ? String basePath = request.getScheme()+"://"
? ? +request.getServerName()+":"+request.getServerPort()
? ? +path+"/";
? ? List<Product>?list?= new ArrayList<Product>();
? ? Product p= new Product();
? ?
? ? p.setName("詩書畫唱CD");
? ? p.setPrice(6.66);
? ? p.setType("CD");
? ? list.add(p);
??
%>

? ? ? ? <meta http-equiv="pragma" content="no-cache">
? ? ? ? <meta http-equiv="cache-control" content="no-cache">
? ? ? ? <meta http-equiv="expires" content="0">
? ? ? ? <meta http-equiv="keywords" content=
? ? ? ? "keyword1,keyword2,keyword3">
? ? ? ? <meta http-equiv="description"?
? ? ? ? content="This is my page">
? ? </head>
? ? <body>
? ? ? ? <table border="1">
? ? ? ? ? ? <tr>
? ? ? ? ? ? ? ? <th>商品名稱</th>
? ? ? ? ? ? ? ? <th>商品價(jià)格</th>
? ? ? ? ? ? ? ? <th>商品類型</th>
? ? ? ? ? ? </tr>
? ? ? ? ? ? <%
? ? ? ? ? ? ? ? for(int i = 0;i < list.size();i ++) {
? ? ? ? ? ? ? ??//取出list中的每樣商品:
? ? ? ? ? ? ? ??Product pro = list.get(i);
? ? ? ? ? ? %>
? ? ? ? ? ? ? ? ? ? <tr>
? ? ? ? ? ? ? ? ? ? ? ? <td><%=pro.getName() %></td>
?<td><%="¥" + pro.getPrice() %></td>
? ? ? ? ? ? ? ? ? ? ? ? <td><%=pro.getType() %></td>
? ? ? ? ? ? ? ? ? ? </tr>
? ? ? ? ? ? <%
? ? ? ? ? ? ? ? }
? ? ? ? ? ? %>
? ? ? ? </table>
? ? </body>
</html>


第一種插入多條的循環(huán)方式:

<%@page import="com.SSHC.Product"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html;
?charset=UTF-8" pageEncoding="UTF-8"%>
<%
? ? String path = request.getContextPath();
? ? String basePath = request.getScheme()+"://"
? ? +request.getServerName()+":"+request.getServerPort()
? ? +path+"/";
? ? List<Product> list = new ArrayList<Product>();
? ?
?String []arrName={"詩書畫唱CD1","詩書畫唱CD2","詩書畫唱CD3"};
? ? Double []arrPrice={6.66,6.66,6.66};
? ? String []arrType={"CD1","CD2","CD3"};
? ??
? ? for(int i=0;i<arrName.length;i++){
? ? Product p= new Product();
? ? p.setName(arrName[i]);
? ? p.setPrice(arrPrice[i]);
? ? p.setType(arrType[i]);
? ? list.add(p);
? ? }
%>

? ? ? ? <meta http-equiv="pragma" content="no-cache">
?<meta http-equiv="cache-control" content="no-cache">
? ? ? ? <meta http-equiv="expires" content="0">
? ? ? ? <meta http-equiv="keywords" content=
? ? ? ? "keyword1,keyword2,keyword3">
? ? ? ? <meta http-equiv="description"?
? ? ? ? content="This is my page">
? ? </head>
? ? <body>
? ? ? ? <table border="1">
? ? ? ? ? ? <tr>
? ? ? ? ? ? ? ? <th>商品名稱</th>
? ? ? ? ? ? ? ? <th>商品價(jià)格</th>
? ? ? ? ? ? ? ? <th>商品類型</th>
? ? ? ? ? ? </tr>
? ? ? ? ? ? <%
? ? ? ? ? ? ? ? for(int i = 0;i < list.size();i ++) {
? ? ? ? ? ? ? ? //取出list中的每樣商品:
? ? ? ? ? ? ? ? Product pro = list.get(i);
? ? ? ? ? ? %>
? ? ? ? ? ? ? ? ? ? <tr>
? ? ? ? ? ? ? ? ? ? ? ? <td><%=pro.getName() %></td>
?<td><%="¥" + pro.getPrice() %></td>
? ? ? ? ? ? ? ? ? ? ? ? <td><%=pro.getType() %></td>
? ? ? ? ? ? ? ? ? ? </tr>
? ? ? ? ? ? <%
? ? ? ? ? ? ? ? }
? ? ? ? ? ? %>
? ? ? ? </table>
? ? </body>
</html>


第二種插入多條的循環(huán)方式:

<%@page import="com.SSHC.Product"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html;
?charset=UTF-8" pageEncoding="UTF-8"%>
<%
? ? String path = request.getContextPath();
? ? String basePath = request.getScheme()+"://"
? ? +request.getServerName()+":"+request.getServerPort()
? ? +path+"/";
? ? List<Product> list = new ArrayList<Product>();
? ??
? ? for(int i=0;i<10;i++){
? ? ? ? Product p= new Product();
? ? p.setName("詩書畫唱CD1"+i);
? ? p.setPrice(6.66+i);
? ? p.setType("CD"+i);
? ? list.add(p);
? ? }
%>

?<meta http-equiv="pragma" content="no-cache">
?<meta http-equiv="cache-control" content="no-cache">
? ? ? ? <meta http-equiv="expires" content="0">
? ? ? ? <meta http-equiv="keywords" content=
? ? ? ? "keyword1,keyword2,keyword3">
? ? ? ? <meta http-equiv="description"?
? ? ? ? content="This is my page">
? ? </head>
? ? <body>
? ? ? ? <table border="1">
? ? ? ? ? ? <tr>
? ? ? ? ? ? ? ? <th>商品名稱</th>
? ? ? ? ? ? ? ? <th>商品價(jià)格</th>
? ? ? ? ? ? ? ? <th>商品類型</th>
? ? ? ? ? ? </tr>
? ? ? ? ? ? <%
? ? ? ? ? ? ? ? for(int i = 0;i < list.size();i ++) {
? ? ? ? ? ? ? ? //取出list中的每樣商品:
? ? ? ? ? ? ? ? Product pro = list.get(i);
? ? ? ? ? ? %>
? ? ? ? ? ? ? ? ? ? <tr>
? ? ? ? ? ? ? ? ? ? ? ? <td><%=pro.getName() %></td>
?<td><%="¥" + pro.getPrice() %></td>
? ? ? ? ? ? ? ? ? ? ? ? <td><%=pro.getType() %></td>
? ? ? ? ? ? ? ? ? ? </tr>
? ? ? ? ? ? <%
? ? ? ? ? ? ? ? }
? ? ? ? ? ? %>
? ? ? ? </table>
? ? </body>
</html>




3、創(chuàng)建一個(gè)Person類,包含姓名、性別和生日屬性,用兩個(gè)循環(huán)拼接table在頁面顯示所有人的信息。


package com.SSHC;
public class Person {
private String name;
private String? sex;
private String? brithday;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getBrithday() {
return brithday;
}
public void setBrithday(String brithday) {
this.brithday = brithday;
}
?
}


<%@page import="com.SSHC.Person"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html;
?charset=UTF-8" pageEncoding="UTF-8"%>
<%
? ? String path = request.getContextPath();
? ? String basePath = request.getScheme()+"://"
? ? +request.getServerName()+":"+request.getServerPort()
? ? +path+"/";
??
?String []arrName={"詩書畫唱1","詩書畫唱2","詩書畫唱3"};
?String []arrSex={"男","男","男"};
String []arrBrithday={"2000-01-01",
"2000-01-02","2000-01-03"};
? ??
List<Person> list = new ArrayList<Person>();
? ? for(int i=0;i<arrName.length;i++){
? ?
? ? //這里的Person p= new Person();
? ? //必須寫里面因?yàn)橐?個(gè)對(duì)象,放到集合里面
? ? //,如果寫外面,就只是一個(gè)對(duì)象,默認(rèn)取每個(gè)數(shù)組的
? ? //最后一個(gè)當(dāng)每個(gè)屬性的值等。
? ?
? ? Person p= new Person();
? ? p.setName(arrName[i]);
? ? p.setSex(arrSex[i]);
? ? p.setBrithday(arrBrithday[i]);
? ? list.add(p);
? ? }
%>

?<meta http-equiv="pragma" content="no-cache">
?<meta http-equiv="cache-control" content="no-cache">
? ? ? ? <meta http-equiv="expires" content="0">
? ? ? ? <meta http-equiv="keywords" content=
? ? ? ? "keyword1,keyword2,keyword3">
? ? ? ? <meta http-equiv="description"?
? ? ? ? content="This is my page">
? ? </head>
? ? <body>
? ? ? ? <table border="1">
? ? ? ? ? ? <tr>
? ? ? ? ? ? ? ? <th>姓名</th>
? ? ? ? ? ? ? ? <th>性別</th>
? ? ? ? ? ? ? ? <th>生日</th>
? ? ? ? ? ? </tr>
? ? ? ? ? ? <%
? ? ? ? ? ? ? ? for(int j = 0;j < list.size();j ++) {
? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? Person pro = list.get(j);
? ? ? ? ? ? %>
? ? ? ? ? ? ? ? ? ? <tr>
? ? ? ? ? ? ? ? ? ? ? ? <td><%=pro.getName() %></td>
?<td><%=pro.getSex() %></td>
? ? ? ? ? ? ? ? ? ? ? ? <td><%=pro.getBrithday() %></td>
? ? ? ? ? ? ? ? ? ? </tr>
? ? ? ? ? ? <%
? ? ? ? ? ? ? ? }
? ? ? ? ? ? %>
? ? ? ? </table>
? ? </body>
</html>


修改端口號(hào)的方法(可能只能用Tomcat的文件夾中的文件來修改):


info
信息
package-info:
https://www.sogou.com/link?url=hedJjaC291OfPyaFZYFLI4KQWvqt63NBNT7omtTV3RuN56nw9WfFxQ..
