欢迎光临散文网 会员登陆 & 注册

Java web:修改Tomcat服务器端口号的方法,jsp,集合,表格,拼接,循环【诗书画唱】

2020-09-03 08:35 作者:诗书画唱  | 我要投稿


自己总结的个人的理解和技巧:


jsp就是用<%  %>等在jsp文件中可以同时写Java和HTML等的代码,<%  %>里面的可以都为Java部分等,而外面的可以为HTML部分的。只要是自己认为应该为Java部分的代码等都用<% %>括起来。有时要把Java中换行的语句等,转换成HTML中的<br/>等,HTML和Java等的代码混合起来等。——诗书画唱


//下面是JSP常用的方法:

    //方法1:拼接html代码(个人理解:用了<%=  %>,

   // <%  %>和HTML等的)

    //方法2:混搭(个人理解:

    //用<%  %>和HTML等的)






下面是常见的防乱码等的JSP代码模板(一般来说把默认生成的jsp模板等中的字符集设置为UTF-8也可能会不容易出现乱码等):

<%@ 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 + "/";

%>

这里的部分复制到b站专栏会让网络有时出错等


<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",

"http://www.bilibili.com"; };


for (int i = 0; i < name.length; i++) {


String html = "<a href='" + hrefName[i] + "'>" + name[i]

+ "</a>";

%>

<%=html%>

<%

}

%>



</body>

</html>   


下面是建jsp代码必要的(至于怎么建可以看我发的教程视频或专栏等):




1、在jsp页面显示一个九九乘法表。



<%@ 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、创建一个商品类,包含名称,价格和类型三个属性,通过两种循环方式拼接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>商品价格</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>





第一种插入多条的循环方式:






<%@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>商品价格</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>




第二种插入多条的循环方式:

<%@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>商品价格</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、创建一个Person类,包含姓名、性别和生日属性,用两个循环拼接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();

    //必须写里面因为要生成3个对象,放到集合里面

    //,如果写外面,就只是一个对象,默认取每个数组的

    //最后一个当每个属性的值等。

   

    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>





修改端口号的方法(可能只能用Tomcat的文件夹中的文件来修改):


info

信息

package-info:

https://www.sogou.com/link?url=hedJjaC291OfPyaFZYFLI4KQWvqt63NBNT7omtTV3RuN56nw9WfFxQ..





Java web:修改Tomcat服务器端口号的方法,jsp,集合,表格,拼接,循环【诗书画唱】的评论 (共 条)

分享到微博请遵守国家法律