目录
一、开发环境安装
首先需要下载并安装一些开发环境,主要有JDK、IDEA、Maven(IDEA自带)、Tomcat以及MySQL8.0
二、新建一个工程
new 一个 empty project
三、添加setting.xml
重新打开刚刚创建的工程再添加setting.xml!!!
1.保存setting.xml到本地路径
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>D:\repository\mavenrepository</localRepository>
<pluginGroups>
</pluginGroups>
<proxies>
<!-- proxy
| Specification for one proxy, to be used in connecting to the network.
|
<proxy>
<id>optional</id>
<active>true</active>
<protocol>http</protocol>
<username>proxyuser</username>
<password>proxypass</password>
<host>proxy.host.net</host>
<port>80</port>
<nonProxyHosts>local.net|some.host.com</nonProxyHosts>
</proxy>
-->
</proxies>
<!-- servers
| This is a list of authentication profiles, keyed by the server-id used within the system.
| Authentication profiles can be used whenever maven must make a connection to a remote server.
|-->
<servers>
<!-- server
| Specifies the authentication information to use when connecting to a particular server, identified by
| a unique name within the system (referred to by the 'id' attribute below).
|
| NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
| used together.
|
<server>
<id>deploymentRepo</id>
<username>repouser</username>
<password>repopwd</password>
</server>
-->
<!-- Another sample, using keys to authenticate.
<server>
<id>siteServer</id>
<privateKey>/path/to/private/key</privateKey>
<passphrase>optional; leave empty if not used.</passphrase>
</server>
-->
</servers>
<mirrors>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
<!-- profiles
| This is a list of profiles which can be activated in a variety of ways, and which can modify
| the build process. Profiles provided in the settings.xml are intended to provide local machine-
| specific paths and repository locations which allow the build to work in the local environment.
|
| For example, if you have an integration testing plugin - like cactus - that needs to know where
| your Tomcat instance is installed, you can provide a variable here such that the variable is
| dereferenced during the build process to configure the cactus plugin.
|
| As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles
| section of this document (settings.xml) - will be discussed later. Another way essentially
| relies on the detection of a system property, either matching a particular value for the property,
| or merely testing its existence. Profiles can also be activated by JDK version prefix, where a
| value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'.
| Finally, the list of active profiles can be specified directly from the command line.
|
| NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact
| repositories, plugin repositories, and free-form properties to be used as configuration
| variables for plugins in the POM.
|
|-->
<profiles>
<!-- profile
| Specifies a set of introductions to the build process, to be activated using one or more of the
| mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>
| or the command line, profiles have to have an ID that is unique.
|
| An encouraged best practice for profile identification is to use a consistent naming convention
| for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
| This will make it more intuitive to understand what the set of introduced profiles is attempting
| to accomplish, particularly when you only have a list of profile id's for debug.
|
| This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.
<profile>
<id>jdk-1.4</id>
<activation>
<jdk>1.4</jdk>
</activation>
<repositories>
<repository>
<id>jdk14</id>
<name>Repository for JDK 1.4 builds</name>
<url>http://www.myhost.com/maven/jdk14</url>
<layout>default</layout>
<snapshotPolicy>always</snapshotPolicy>
</repository>
</repositories>
</profile>
-->
<profile>
<id>development</id>
<activation>
<jdk>18</jdk>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<maven.compiler.source>18</maven.compiler.source>
<maven.compiler.target>18</maven.compiler.target>
<maven.compiler.compilerVersion>18</maven.compiler.compilerVersion>
</properties>
</profile>
<!--
| Here is another profile, activated by the system property 'target-env' with a value of 'dev',
| which provides a specific path to the Tomcat instance. To use this, your plugin configuration
| might hypothetically look like:
|
| ...
| <plugin>
| <groupId>org.myco.myplugins</groupId>
| <artifactId>myplugin</artifactId>
|
| <configuration>
| <tomcatLocation>${tomcatPath}</tomcatLocation>
| </configuration>
| </plugin>
| ...
|
| NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to
| anything, you could just leave off the <value/> inside the activation-property.
|
<profile>
<id>env-dev</id>
<activation>
<property>
<name>target-env</name>
<value>dev</value>
</property>
</activation>
<properties>
<tomcatPath>/path/to/tomcat/instance</tomcatPath>
</properties>
</profile>
-->
</profiles>
<!-- activeProfiles
| List of profiles that are active for all builds.
|
<activeProfiles>
<activeProfile>alwaysActiveProfile</activeProfile>
<activeProfile>anotherAlwaysActiveProfile</activeProfile>
</activeProfiles>
-->
</settings>
2.导入项目
四、创建maven模块
new Module->
五.添加Tomcat
1.下载好安装包
2.分别点击:
六、导入servlet依赖包
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
改完要按右上角:
也可以按这个来刷新导入:
如果IDEA中的pom.xml文件变成了橙色:
在pom.xml文件上单击鼠标右键,然后在弹出框中点击+ Add as Maven Project
就可以了
七、运行--》配置成功
八、用Servlet输出html语句
response.getWriter().println("Hello!");
若报错:
则将这些地方改为8:
Servlet访问路径=工程路径(localhost+端口号+包名)+Servlet的value值,所以:
九、网页动态化(每个用户访问到不同数据)
1.标签的值需要从数据库取
2.jsp可以访问数据库取值, jsp可以嵌入java代码,通过JDBC访问数据库
3.单一职责原则:一个类不能太累了,因此我们可以用Servlet转发jsp页面
4. jsp页面负责html网页相关的,Servlet负责请求java逻辑相关的代码
//通过Servlet转发jsp页面
request.getRequestDispatcher("index.jsp").forward(request,response);
Redeploy运行:(中文乱码了..懒得改)
十、从数据库中请求用户名的数据库
//从数据库中请求用户名的数据库
String name="Name";
request.setAttribute("假如是数据库中的name",name);
request.getRequestDispatcher("index.jsp").forward(request,response);
<%= request.getAttribute("name")%>
十一、Java JDBC连接数据库
了解JDBC连接数据库步骤
1.打开数据库连接
2.SQL语句请求数据库得到数据
3.数据处理封装
4.关闭数据库同时我们还要遵循单一职责原则:一个类不能太累了,因此需要把连接数据库的功能单独分离出去,不再用Servlet连接增加负担,那么应该怎么做呢?
1.这时我们需要新建一个包,把所有与数据库操作有关的放到这个包下,这个包通常叫dao**:
2.假如我们需要访问用户数据库,我们可以新建一个接口为UserDao
3. 每一个模块需要一个接口+一个实现类
(1)接口
public String findUser();
(2)实现类,在dao包下新建一个包为impl,在impl包下新建java类为UserDaoImpl
public class UserDaoImpl implements UserDao {
public String findUser() {
// todo 数据库请求获取用户名
String name="从数据库中取出来的testName";
return name;
}
}
4.修改Servlet代码
运行可以访问到index的数据
十二、Controller层
MyServlet可以放入Controller层
因此我们用controller层接收请求,dao层连接数据库,为了更方便,后面我们会用SpringMVC替换MyServlet接收请求,用MyBatis替换JDBC连接数据库
上课内容总结 及 参考实践该博客: