【高级网络程序设计】Week3-2 Servlet

一、 What are servlets?

1. 定义

(1)Servlets are Java’s answer to CGI:

programs that run on a web server acting as middle layer between HTTP request and databases or other applications.
Used for client requests that cannot be satisfied using pre-built (static) documents.
Used to generate dynamic web pages in response to client.

(2)图解

Web BrowserSending Requests to a Web Server
Web Server

hold/return static web pages

– e.g. index.html does not respond to user input.

Server Side Programs

different technologies written in various languages

 – e.g. CGI scripts, Java Servlets (and JSPs – later), PHP scripts
 – Call to http://localhost:8080/servlet/myhelloservlet.HelloServlet
 – Not web-browsing but executing program (servlet)
Dynamic response– database lookup, calculation etc.
We’ll look at the Java solutionhow to write servlets; how the container (Tomcat) works.

2. A general purpose Web Server

二、Simple Example

ignore how the Container finds the servlet (via the deployment descriptor web.xml).
<form action=“http://server.com/ExecuteServlet”>
<input type=“submit” value = “press for servlet”>
</form>

1.  A Basic Servlet

import jakarta.servlet.*;
import jakarta.servlet.http.*;
import java.io.*;
//import jakarta.servlet.*:imports classes in this directory, but not in sub-directories
public class HelloServlet extends HttpServlet {public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {response.setContentType("text/html");//设置响应文件类型、响应式的编码形式PrintWriter out = response.getWriter();//获取字符输出流out.println(“<html><body>Hello!</body></html>”);out.close();}
}

2.  Echo Servlet

import jakarta.servlet.*;
import jakarta.servlet.http.*;
import java.io.*;
public class GetEchoServlet extends HttpServlet {public void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException, ServletException {String userName = request.getParameter(“fname”);//获取form中输入的参数response.setContentType("text/html");//设置响应文件类型、响应式编码格式PrintWriter out = response.getWriter();//获取字符输出流out.println(“<html><body>Hello”);if (userName != null) out.println(userName);else out.println(“mystery person”);out.println(“</body></html>”);out.close();}
}

3. BMI Servlet

- HTML
//Page that asks for weight (kg) and height (cm) :Write the HTML and the HTTP Request (GET)
<form method=“GET”action=“http://server.com/BMIServlet”>
<input type=“text” name=“weight”/>weight<br>
<input type=“text” name=“height”/>height<br>
<input type=“submit” value=“send”>
</form>

- Servlet


import jakarta.servlet.*;
import jakarta.servlet.http.*;
import java.io.*;
public class BMIServlet extends HttpServlet {public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {String ht = request.getParameter(“height”);int height = Integer.parseInt(ht);double weight = Double.parseDouble(request.getParameter(“weight”));double ht_squared = (height/100.0)*(height/100.0);response.setContentType("text/html");PrintWriter out = response.getWriter();out.println(“<html><body><br>”);out.println(“Your BMI is: ” + weight/ht_squared + “<body></html>”);out.close();}
}

4. Name-salary Servlet

import jakarta.servlet.*;
import jakarta.servlet.http.*;
import java.io.*;
public class HelloServlet extends HttpServlet {public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {String name = request.getParameter("name");int salary = Integer.parseInt(salary);response.setContentType("text/html");PrintWriter out = response.getWriter();out.println("<html><body><br>");out.println("Hello,"+name);out.println("Your salary is"+salary);out.println("<body><html>");out.close();} // end of method
} // end of class

三、Servlet Key Points

1. Servlets: Key points

NO main method public static void main(String[] args)
NO constructor

There is one (default) constructor but the developer should never write an explicit constructor

 – Why——servlet lifecycle

Two key (service) methodsdoGet(); doPost()

2. Finding things

Tracing the user data
– e.g. name attribute inside an HTML element
– name in HTTP request message
– argument in request.getParameter(...)
 String(int/double required)

then have to use appropriate method on this String to convert

- Integer.parseInt();

- Double.parseDouble();

Finding the servlet
<form> tag action attribute e.g. <FORM action=“servlet/myServlet” ...>
– Used by deployment descriptor, web.xml (see later), to map to the corresponding servlet class.

3. JavaBeans, JSPs and Servlets

Although a servlet can be a completely self-contained program, to ease server-side programming, generating content should be split into
The business logic (content generation), which governs the relationship between input, processing, and output.
The presentation logic (content presentation, or graphic design rules), which determines how information is presented to the user.
controllerthe servlet handles the HTTP protocol and coordination of which other servlets and auxiliary class methods to call
modelJava classes/JavaBeans handle the business logic
viewJava Server Pages handle presentation logic

4. Advantages of Servlets over CGI

Efficient
– Servlets run in the JVM. Each request is serviced using a thread rather than a new process (so lower overhead).
- Though some scripting languages, e.g. perl on certain web servers do this now.
Convenient– Provides infrastructure that parses and decodes HTML forms.
Powerful
– Can communicate directly with web server.
– Multiple servlets can share database connections.
– Simplifies session tracking.
Portable– Written in Java and follows standard API.
Secure
– CGI often executed using O/S shells, which can cause many security breaches.
– Array checking & exception handling is automatic in Java.
Inexpensive
– Many Java web servers are freely available.

四、Servlets in Detail

1. A general purpose Web Server

2. What servlets do

 requestRead any data sent by the user
Look up information embedded in HTTP request
Generate results.
response
Format results inside a document (e.g. HTML, XML, GIF, EXCEL).
Set appropriate HTTP response parameters.
Send the document back to the client.

3. Typical generic servlet code

import java.io.*;
import jakarta.servlet.*;
public class AnyServlet extends GenericServlet {public AnyServlet() {} // constructor – BUT USE THE DEFAULT// NEVER ANY NEED TO WRITE ONE//ONLY creates an object, becomes a “proper” servlet after init().public void init(ServletConfig config) throws ServletException;// The method is actually called by container when servlet is// first created or loaded.public void service(ServletRequest req, ServletResponse res)throws ServletException, IOException;// Called by a new thread (in the container) each time a// request is received.public void destroy();// Called when servlet is destroyed or removed.
}

4. A Servlet is “deployed” in a container

• A program that receives (e.g. HTTP) requests to servlets from a web server application:
– finds the servlet (and loads, calls constructor & init() if not ready);
– creates or reuses a thread that calls service() method of chosen servlet;
– creates & passes request and response objects to chosen servlet;
– passes the response (e.g. HTTP response) back to the web server application; kills servlet thread or recycles into thread pool; and deletes request and response objects.
• More generally, manages the life cycle of its servlets:
Calls constructor, init(), service(), destroy().
– Also invokes methods of listening classes that a servlet implements (out of scope here).
• It has a main() and is working “all the time”.
• Also provides:
– Declarative security using settings in Deployment Descriptor (DD).
– JSP support.

5. Dissecting the Container’s actions

- HTTP request:
GET   /myServlet/BMIInfo   height=156&name=paula+fonseca   HTTP/1.1
- Servlet:
public class BMIServlet extends HttpServlet {
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws
IOException, ServletException {
                // ...
                String ht = request.getParameter(“height”);
                // ...
        }
}

6. The servlet life cycle

7. methods & inheritance

8. Servlet’s Life cycle

Constructor (no arguments)
– not written or called by a developer
– called by the container
public void init()
– called after constructor
– called once, at the beginning (so potentially useful for initialisation of, e.g. databases)
– can be overridden by the developer
public void service(…)
rarely overridden by thedeveloper
– called everytime

public void doGet() /

public void doPost()

must be written by the developer
– must match the HTTP method in <form> in the HTML
public void destroy()

– must be written by the developer

五、 Configuration Servlets To Run In Tomcat

1. Mapping names using the Deployment Descriptor (DD)

//For each servlet in the web application.
//Internal name of servlet can be “anything” following XML rules.
<web-app ...><servlet><servlet-name>…</servlet-name>//maps internal name to fully qualified class name (except without .class)<servlet-class>…</servlet-class>//maps internal name to public URL name e.g. /makebooking</servlet><servlet-mapping><servlet-name>…</servlet-name><url-pattern>…</url-pattern ></servlet-mapping>
...
</web-app>

2. Servlet Mapping Examples

- HTML:
<FORM method=“post” action=“/servlet/MyTest.do”>
- Server (webapps):
WEB-INF/classes/Echo.class
<servlet>
        <servlet-name>......</servlet-name>
        <servlet-class>.....</servlet-class>
</servlet>
<servlet-mapping>
        <servlet-name>......</servlet-name>
        <url-pattern>.......</url-pattern >
</servlet-mapping>
- HTML:
<FORM method=“post” action=“/servlet/Test”>
- Server (webapps):
WEB-INF/classes/foo/Name.class
<servlet>
        <servlet-name>......</servlet-name>
        <servlet-class>.....</servlet-class>
</servlet>
<servlet-mapping>
        <servlet-name>......</servlet-name>
        <url-pattern>.......</url-pattern >
</servlet-mapping>

3. Example: A Small Form

//SmallForm.html
<html><title>Sending Form Information to a Servlet</title><body><form action="http://localhost:8080/servlet/elem004.ProcessSmallForm"method="post">//Can use absolute or relative URLs or pre-configured names.Please input your login: <br><input type="text" name="Login"><input type="submit" value="Login"></form></body>
</html>//the Deployment Descriptor (web.xml) and the servlet
<servlet><servlet-name>smallForm</servlet-name><servlet-class>SmallFormServlet</servlet-class>
</servlet>
<servlet-mapping><servlet-name>smallForm</servlet-name><url-pattern>/servlet/elem004.ProcessSmallForm</url-pattern>
</servlet-mapping>

4. Putting everything in the right place

Level 1WEB-INF (folder) and .html, .jsp
Level 2(inside WEB-INF folder): web.xml and classes (folder)
Level 3 (inside classes folder): servlet .class files (and other “business” class files e.g. JavaBeans)

5. Servlet initialisation & Servlet Configuration object

• Only one servlet instance is created: each request is serviced by a separate thread in the container.
• Prior to initialisation, the ServletConfig object is created by the container:
– one ServletConfig object per servlet;
– container uses it to pass deploy-time information to the servlet (data you do not want to hard code into the servlet, e.g. the DB name);
– the names are specified in the DD.
• Parameters are set in a server-specific manner, e.g.
– in a file called web.xml (for Tomcat);
– in a file called resin.config (for Resin).
• Parameters do not change while servlet is deployed and running:
– like constants;
– if servlet changes, then need to redeploy.

6. Example: DD’s init parameters (web.xml for Tomcat)

<web-app xmlns=“http://java.sun.com/xml/ns/j2ee”xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”xsi:schemaLocation=“http//java.sun.com/xml/ns/j2ee/web-app_2.4.xsd”version=“2.4”><servlet><servlet-name>Hello World Servlet</servlet-name><servlet-class>S1</servlet-class><init-param><param-name>lecturersEmail</param-name><param-value>paula.fonseca@qmul.ac.uk</param-value></init-param>//Container reads these & gives them to ServletConfig object.</servlet>
</web-app>
out.println(getServletConfig().getInitParameter(“lecturersEmail”));
Returns the servlet’s ServletConfig object (all servlets have this method).
Getting a parameter value from the ServletConfig object; this code is in servlet.

7. Creating a servlet: ServletConfig and init(…)

Step 1container reads the deployment descriptor
Step 2container creates new ServletConfig object
Step 3
container creates name/value pair (Strings) for each servlet init-param
Step 4
container passes references to these to the ServletConfig object
Step 5container creates new instance of the servlet class
Step 6
container calls servlet’s init() method passing in reference to the ServletConfig object

六、Thread Safety And Putting Things Together

1. Instance Variables

public class ExampletServlet extends HttpServlet {
private int age;
public void init() { age = 0; }
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws
IOException, ServletException {
age = Integer.parseInt(request.getParameter(“age”));
response.setContentType(“text/html”);
PrintWriter out = response.getWriter();
out.println(“<HTML><BODY>You are ” + age + “ weeks old”);
out.println(“</BODY></HTML>”);
out.close();
}
}

2. The 3 Threads access same Resources

• We don’t know whose age will be displayed!
• Solution 1:
– Never use instance variables in servlets (some books say you
can’t – what they mean is you shouldn’t!).
– Find a way to save information (state) about each user (i.e.
each request) – we’ll see how later …
• Solution 2:
Synchronisation – a lock that makes a variable thread-safe

3. Access – introducing the ServletContext object

Servlet
ServletConfig object – one per servlet (or JSP)
– contains init params
– all requests made to a servlet can access this object
• Web application
ServletContext object – one per web application
• Web applications normally have several servlets/JSPs.
– Used to access web application parameters that need to be seen by all servlets/JSPs in the application.
• A misnomer, as it relates not to a servlet but to the set of servlets and JSPs in the web application.
The web application’s DD specifies the context parameters

<web-app ...> ...<servlet><servlet-name>...</servlet-name><servlet-class>...</servlet-class><init-param><param-name>...</param-name><param-value>...</param-value></init-param></servlet> ... + other servlets<context-param><param-name>HOP_Email</param-name><param-value>g.tyson@qmul.ac.uk</param-value></context-param>
...
</web-app>

Note: Not inside any servlet. These are parameter namevalue pairs: both are strings.

ServletContext object created and set up when web application is deployed.

4. To access web app parameters in servlet code

ServletContext ctx = getServletContext();
out.println(ctx.getInitParameter(“HOP_Email”));
Context parameters generally more commonly used than Config.
– Typical use (of former) is a DB lookup name.
• Can access ServletContext,
– directly: getServletContext().getInitParameter(…)
– from ServletConfig: getServletConfig().getServletContext().getInitParameter(…)
– Latter is useful if in a method of an auxiliary class e.g. a JavaBean, and only the ServletConfig object has been passed as a parameter.
Same name for get method as when accessing ServletConfig object.

5. ServletContext also has attributes

• Parameters are name-value pairs, where both name and value are strings.
• Attributes are name-value pairs where the name is a String, but the value is an object (that may not be a String).
– accessed by getAttribute(String);
– set by setAttribute(String,Object).
• Running code prior to invoking any servlet in the application:
– E.g. to turn sets of parameters into attribute objects,
• so all servlets only need to deal with objects;
• and don’t have to read the context parameters.
– Implement a ServletContextListener (out of scope here)

6. Servlets – The Basics: Key Points & What we can’t do yet

How to call a servlet in an HTML formWhat a deployment descriptor does
How to write a servletWhere to deploy files
How to access client informationServlet life-cycle
Initialisation
ServletConfig
ServletContext
Have a conversation with a client
– Shopping basket
Session object
Run any code before a servlet starts
– E.g. Database set up
Listeners

Send client information, or control,

to another servlet/JSP

– Could be in another web server
Redirect and forward

7. Extracting unknown parameters and multiple values

String getParameter(String)
parameter name is known:
– returns null if unknown parameter;
– returns "" (i.e. empty string) if parameter has no value.
Enumeration getParameterNames() obtain parameter names
String[] getParameterValues(String) 

obtain an array of values for each one:

– returns null if unknown parameter;
– returns a single string ("") if parameter has no values. Case sensitive.

8. Example: A Big Form

//BigForm.html
<form action="http://localhost:8080/servlet/elem004.ProcessBigForm"method="post">Please enter: <br><br>Your login: <input type="text" name="Login"> <br><br>Your favourite colour:<input type="radio" name="Colour" value="blue">Blue<input type="radio" name="Colour" value="red">Red<input type="radio" name="Colour" value="green">Green <br><br>//Single-value parameters.Which of these courses you are taking: <br><input type="checkbox" name="Course" value="elem001">ELEM001 <br><input type="checkbox" name="Course" value="elem002">ELEM002 <br><input type="checkbox" name="Course" value="elem003">ELEM003 <br><input type="checkbox" name="Course" value="elem004">ELEM004 <br><input type="submit" value="Send to Servlet">//Multiple-value parameter.
</form>
//After BigForm is processed.getParameterNames() returns parameters in no particular order.//ProcessBigForm.java
//More code here ...out.println("<table border=1>");// Obtain all the form’s parameters from the request object.Enumeration paramNames = req.getParameterNames();while (paramNames.hasMoreElements()) {String paramName = (String) paramNames.nextElement();// Obtain values for this parameter and check how many there are.String[] paramValues = req.getParameterValues(paramName);if (paramValues.length == 1) { // a single valueString paramVal = req.getParameter(paramName);out.println("<tr><td>" + paramName +"</td><td>"+ paramVal + "</td></tr>");}else { // If several values print a list in the table.out.println("<tr><td>" + paramName +"</td><td><ul>");for (int i = 0; i < paramValues.length; i++)out.println("<li>" + paramValues[i] + "</li>");out.println("</ul></td></tr>");}}out.println("</table>");out.close();
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/159257.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

人工智能和AR/VR:AI在AR和VR中扮演什么角色?行业应用有哪些?

增强现实技术和虚拟现实技术&#xff08;AR/VR&#xff09;发展前景广阔&#xff0c;备受各大企业关注。事实上&#xff0c;近四分之三的行业领导者表示&#xff0c;他们预计这些沉浸式技术将于未来五年内成为主流。高盛公司报告称&#xff0c;到2025年&#xff0c;AR/VR行业值…

Golang版本处理Skywalking Trace上报数据

Tips: 中间记录了解决问题的过程&#xff0c;如不感兴趣可直接跳至结尾 首先去es里查询skywalking trace的元数据 可以拿到一串base64加密后的data_binary(直接解密不能用&#xff0c;会有乱码&#xff0c;可参考https://github.com/apache/skywalking/issues/7423) 对data_b…

图解系列--密钥,随机数,应用技术

密钥 1.生成密钥 1.1.用随机数生成密钥 密码学用途的伪随机数生成器必须是专门针对密码学用途而设计的。 1.2.用口令生成密钥 一般都是将口令输入单向散列函数&#xff0c;然后将得到的散列值作为密钥使用。 在使用口令生成密钥时&#xff0c;为了防止字典攻击&#xff0c;需要…

笔记本只使用Linux是什么体验?

笔记本只使用Linux是什么体验&#xff1f; 之后安了Windows双系统之后也不怎么想再进Windows了。 开发环境就不用说了&#xff0c;Linux下配各种开发环境都方便的多&#xff0c;当然你要用 vs 那还是乖乖回 Windows 吧。 最近很多小伙伴找我&#xff0c;说想要一些Linux的资…

详解Java的static关键字

文章目录 &#x1f384;静态方法&#x1f33a;静态方法和非静态方法对比&#x1f6f8;静态方法实例&#x1f6f8;非静态方法实例 &#x1f339;static关键字⭐static变量⭐static代码块 &#x1f384;静态方法 不依赖于对象实例&#xff1a;静态方法不需要依赖于任何对象实例&…

java的继承特性和方法重写

java的继承特性和方法重写 Java的继承特性是一种面向对象编程的重要概念&#xff0c;它允许我们基于已有的类创建新的类&#xff0c;并且保留了已有的类的一些特性。这是通过使用"继承"这个关键词来实现的&#xff0c;新创建的类称为子类&#xff08;subclass&#…

Spark---核心介绍

一、Spark核心 1、RDD 1&#xff09;、概念&#xff1a; RDD&#xff08;Resilient Distributed Datest&#xff09;&#xff0c;弹性分布式数据集。 2&#xff09;、RDD的五大特性&#xff1a; 1、RDD是由一系列的partition组成的 2、函数是作用在每一个partition(split…

Linux 磁盘/分区/修复 命令

目录 1. lsblk&#xff08;list block devices&#xff09; 2. fdisk&#xff08;fragment disk&#xff09; 3. gdisk 4. mkfs&#xff08;make filesystem&#xff09; 5. df&#xff08;display file-system disk space usage&#xff09; 6. du 7. fsck&#xff08;file-sy…

Android修行手册-POI操作Excel文档

Unity3D特效百例案例项目实战源码Android-Unity实战问题汇总游戏脚本-辅助自动化Android控件全解手册再战Android系列Scratch编程案例软考全系列Unity3D学习专栏蓝桥系列ChatGPT和AIGC &#x1f449;关于作者 专注于Android/Unity和各种游戏开发技巧&#xff0c;以及各种资源分…

从零开始学习typescript——变量

就像我们在学校学习语文、英文时候一样&#xff0c;最开始学习的是语法&#xff0c;要知道基础的结构。 图片中包含 变量、标识符、数据类型、运算符、字面量、表达式、控制语句等语法 变量 变量是使用给定的符号名在内存中申请存储地址&#xff0c;并且可以容纳某个值。 语…

多篇论文介绍-可变形卷积

01 具有双层路由注意力的 YOLOv8 道路场景目标检测方法 01 摘要: 随着机动车的数量不断增加&#xff0c;道路交通环境变得更复杂&#xff0c;尤其是光照变化以及复杂背景都会干扰目标检测算法的准确性和精度&#xff0c;同时道路场景下多变形态的目标也会给检测任务造成干扰&am…

浅谈低压绝缘监测及定位系统在海上石油平台的研究与应用

安科瑞 华楠 摘要&#xff1a;海上石油平台低压系统与陆地电力系统有很大区别&#xff0c;其属于中性点绝缘系统&#xff0c;在出现单相接地故障时&#xff0c;系统允许带故障正常运行2 h&#xff0c;保证海上重要电气设备不会立即关停。现以渤海某海上平台为例&#xff0c;其…

可上手 JVM 调优实战指南

文章目录 为什么要学 JVM一、JVM 整体布局二、Class 文件规范三、类加载模块四、执行引擎五、GC 垃圾回收1 、JVM内存布局2 、 JVM 有哪些主要的垃圾回收器&#xff1f;3 、分代垃圾回收工作机制 六、对 JVM 进行调优的基础思路七、 GC 情况分析实例八、最后总结 全程可上手JVM…

steam游戏找不到steam_api64.dll,分享三个有效的解决方法

在现代科技发展的时代&#xff0c;游戏已经成为了许多人生活中不可或缺的一部分。而Steam作为全球最大的数字发行平台之一&#xff0c;拥有着庞大的游戏库和活跃的用户群体。然而&#xff0c;在使用Steam时&#xff0c;有些用户可能会遇到Steam_api64.dll丢失的问题&#xff0c…

我在Vscode学OpenCV 几何变换(缩放、翻转、仿射变换、透视、重映射)

几何变换指的是将一幅图像映射到另一幅图像内的操作。 cv2.warpAffine&#xff1a;使用仿射变换矩阵对图像进行变换&#xff0c;可以实现平移、缩放和旋转等操作。cv2.warpPerspective&#xff1a;使用透视变换矩阵对图像进行透视变换&#xff0c;可以实现镜头校正、图像纠偏等…

Positive证书:最便宜的SSL证书

在当今数字化的时代&#xff0c;网上交易和信息传输已经成为我们生活中不可或缺的一部分。然而&#xff0c;随着网络犯罪的增加&#xff0c;确保在线信息的安全性变得尤为重要。Positive证书作为一种经济实惠的数字证书&#xff0c;在提供有效安全性的同时&#xff0c;为用户提…

C# Onnx 特征匹配 DeDoDe 检测,不描述---描述,不检测

目录 介绍 效果 模型信息 项目 代码 下载 介绍 github地址&#xff1a;https://github.com/Parskatt/DeDoDe DeDoDe &#x1f3b6; Detect, Dont Describe - Describe, Dont Detect, for Local Feature Matching The DeDoDe detector learns to detect 3D consisten…

Redis主从,缓存击穿,雪崩,哨兵等问题

Redis的性能管理&#xff1a; Redis的数据缓存在内存当中 INFO memory used_memory:853808 Redis中数据占用的内存 used_memory_rss:3715072 Redis向操作系统申请的内容 used_memory_peak:853808 Redis使用的内存的峰值 系统巡检&#xff1a;硬件巡检&#xff0c;数据库…

解析IBM SPSS Statistics 26 forMac/win中文版:全面统计分析解决方案

作为一款强大的统计分析软件&#xff0c;IBM SPSS Statistics 26&#xff08;spss统计软件&#xff09;在全球范围内被广泛使用。无论是学术研究、市场调研还是商业决策&#xff0c;SPSS统计软件都能提供全面的解决方案&#xff0c;帮助用户快速、准确地分析数据。 首先&#…

第二证券:什么是权重股?权重股可以长期持有吗?

权重版块是指该版块股票市值巨大&#xff0c;在股票总市值中的比重很大&#xff08;即权重很大&#xff09;&#xff0c;其涨跌对股票指数的影响很大的一个版块&#xff0c;比方&#xff0c;商场上的证券、钢铁、银行、保险、石油等板块的个股。 权重股适合长时间持有&#xf…