search
HomeDatabaseMysql TutorialServlet/JSP配置详解_MySQL

Servlet/JSP配置详解_MySQL

Jun 01, 2016 pm 02:07 PM
cjdkdocumentDetailed explanationConfiguration

servlet


  初学者问的诸如:《怎样配置环境变量》《怎样运行Servlet》啊?这样的问题太多了,现在我写一个初学者入门必读,以便对初学者有指导作用!

  首先是下载工具:

  我建议初学者用Editplus+JDK,我觉得如果用例如JB,Eclipse,JCreator,虽然刚开始的时候比较方便,但是确使初学者门不知道怎样配置环境变量,从而难以达到知其然,知其所以然的地步。

  可以通过如下地址下载:

Editplus(最新版本是v2.11): http://count.skycn.com/softdown.php?id=3641&url=http://sc-http.skycn.net/down/epp211a_cn.exe (要照注册码就自己找吧,网上很多的)

JDK(最新版本是Java2sdk1_4_2): http://count.skycn.com/softdown.php?id=3116&url=http://sc-http.skycn.net/down/j2sdk-1_4_2-windows-i586.exe (这是For Windows)

  然后就是安装JDK,我是把它装到从c:\JDK目录下面。

  然后设置CLASSPATH的问题了:

  正如操作系统利用PATH来搜索可执行程序一样,Java运行环境也会遍历CLASSPATH来查找类,即便是HelloWorld这样简单的程序,JVM也会遍历CLASSPATH定义的每一个路径,直到找到相应的文件为止。

  相信大家用的系统不是2k就是XP,然后就应当如下设置Path:

  我的电脑->属性->高级->环境变量

  然后在环境变量的Path后面追加: C:\JDK\bin;.;C:\JDK\lib

  也可以这样配置:C:\JDK\bin;.;C:\JDK\lib\dt.jar;C:\JDK\lib\tools.jar

  ★记住:环境变量中的 . 切记不能少,它表示当前路径,如果少掉出现的错误等会就说!

  dt.jar是关于运行环境的类库,tools.jar是关于一些工具的类库

  如果没有配置:C:\JDK\bin,则会出现 “ javac' 不是内部或外部命令,也不是可运行的程序或批处理文件。”这样的错误。

  下面我们就写一个示例程序:

  打开Editplus,新建一个Java文件,请照着如下输入,要一字不漏,并且分清大小写:

public class HelloWorld{
public static void main(String[] args){
System.out.println("Hello,World!");
}
}

  然后把这个文件保存(ctrl + s)到HelloWorld.java,Java是分大小写的,所以大小写一定要分清,是HelloWorld.java不是helloworld.java或者其他。

  运行:开始->运行->cmd

  在控制台中把目录切换到当前目录:

javac HelloWorld.java
java HelloWorld

  你就会在控制台上看见输出的Hello,World!


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><head><title>");
out.println("This is my first Servlet");
out.println("</title></head><body>");
out.println("<h1>Hello,World!</h1>");
out.println("</body></html>");

}
}

  然后照样用javac HelloWorld.java来编译这个文件,如果出现无法import javax.servlet.*

  那么就是应该把C:\Tomcat\common\lib里面的servlet.jar文件拷贝到C:\JDK\jre\lib\ext中,再次编译,就没有问题了!

  然后在Tomcat目录里面的C:\Tomcat\webapps\ROOT里面按如下的文件结构:

ROOT\index.html
ROOT\welcom.jsp
ROOT\WEB-INF\lib\MyServlet.jar(如果你的servlet的.class打成了.jar文件,则放在lib下面)
ROOT\WEB-INF\classes\HelloWorld.class(把上面生成的HelloWorld.class文件放在这个里面)

  然后在浏览器中输入 http://localhost:8080/servlet/HelloWorld ,于是Server众望所归的报错了:Error 404--Not Found

  怎么回事呢?

  Servlet必须使用C:\Tomcat\webapps\ROOT\WEB-INF这个目录下面的web.xml文件进行注册,用EP打开这个web.xml文件,在里面加入:

<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/servlet/helloworld</url-pattern>
</servlet-mapping>

  这样的结构

<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>

  表示指定包含的servlet类。而以下的结构:

<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/servlet/HelloWorld</url-pattern>
</servlet-mapping>

   表示指定HelloServlet应当映射到哪一种URL模式。

  在修改web.xml完毕过后,重新启动Server,然后再输入 http://localhost:8080/servlet/HelloWorld ,那么偌大一个Hello,World!等着你呢。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What are the different storage engines available in MySQL?What are the different storage engines available in MySQL?Apr 26, 2025 am 12:27 AM

MySQLoffersvariousstorageengines,eachsuitedfordifferentusecases:1)InnoDBisidealforapplicationsneedingACIDcomplianceandhighconcurrency,supportingtransactionsandforeignkeys.2)MyISAMisbestforread-heavyworkloads,lackingtransactionsupport.3)Memoryengineis

What are some common security vulnerabilities in MySQL?What are some common security vulnerabilities in MySQL?Apr 26, 2025 am 12:27 AM

Common security vulnerabilities in MySQL include SQL injection, weak passwords, improper permission configuration, and unupdated software. 1. SQL injection can be prevented by using preprocessing statements. 2. Weak passwords can be avoided by forcibly using strong password strategies. 3. Improper permission configuration can be resolved through regular review and adjustment of user permissions. 4. Unupdated software can be patched by regularly checking and updating the MySQL version.

How can you identify slow queries in MySQL?How can you identify slow queries in MySQL?Apr 26, 2025 am 12:15 AM

Identifying slow queries in MySQL can be achieved by enabling slow query logs and setting thresholds. 1. Enable slow query logs and set thresholds. 2. View and analyze slow query log files, and use tools such as mysqldumpslow or pt-query-digest for in-depth analysis. 3. Optimizing slow queries can be achieved through index optimization, query rewriting and avoiding the use of SELECT*.

How can you monitor MySQL server health and performance?How can you monitor MySQL server health and performance?Apr 26, 2025 am 12:15 AM

To monitor the health and performance of MySQL servers, you should pay attention to system health, performance metrics and query execution. 1) Monitor system health: Use top, htop or SHOWGLOBALSTATUS commands to view CPU, memory, disk I/O and network activities. 2) Track performance indicators: monitor key indicators such as query number per second, average query time and cache hit rate. 3) Ensure query execution optimization: Enable slow query logs, record and optimize queries whose execution time exceeds the set threshold.

Compare and contrast MySQL and MariaDB.Compare and contrast MySQL and MariaDB.Apr 26, 2025 am 12:08 AM

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

How does MySQL's licensing compare to other database systems?How does MySQL's licensing compare to other database systems?Apr 25, 2025 am 12:26 AM

MySQL uses a GPL license. 1) The GPL license allows the free use, modification and distribution of MySQL, but the modified distribution must comply with GPL. 2) Commercial licenses can avoid public modifications and are suitable for commercial applications that require confidentiality.

When would you choose InnoDB over MyISAM, and vice versa?When would you choose InnoDB over MyISAM, and vice versa?Apr 25, 2025 am 12:22 AM

The situations when choosing InnoDB instead of MyISAM include: 1) transaction support, 2) high concurrency environment, 3) high data consistency; conversely, the situation when choosing MyISAM includes: 1) mainly read operations, 2) no transaction support is required. InnoDB is suitable for applications that require high data consistency and transaction processing, such as e-commerce platforms, while MyISAM is suitable for read-intensive and transaction-free applications such as blog systems.

Explain the purpose of foreign keys in MySQL.Explain the purpose of foreign keys in MySQL.Apr 25, 2025 am 12:17 AM

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.