I have taken a lot of detours on this!
The compiler uses Intelij+ and the server uses Tomcat.
In order to take care of some students who really don’t know anything, I took screenshots of every step, multi-picture warning!
1. Create a new project
To create a new project, choose ordinary java EE.
My project is called FirstConnect, and the built project should look like this.
2. Solve the problem of getparameter becoming popular
Find the path where you built the project in windows, and create two new folders.
Create a new folder \lib under the root directory
Create a new folder classes under web\WEB-INF
Next, find your Tomcat directory and open lib to copy jsp-api.jar and servlet-api.jar
Copy to the lib directory we created under the project root directory
Next go back to intelij, click file->Project Structure
Next Modules->Dependencies->plus sign, 1.JARS or directories
Select the two jar packages in our project/lib and add them
The problem of getparameter becoming popular is solved!
(In fact, another solution is to add the package in tomcat's lib in the libraries, but I think it is better to copy it into the project and add dependencies insurance)
3.intelij creates package, servlet
Do not close the project structure interface, it has not been set up yet! Modules->Paths->Use module compile output path Select the classes folder we created under WEB-INF. The output path and Test output path are temporarily set to this.
Next, go back to the project directory, right-click on the src folder to create a new package (building a package is a good habit)
It is still the src folder or our package folder, right-click to create a new servlet (otherwise there may be no option to create a new servlet)
When creating a new servlet, you must check this annotated class, otherwise you will wait to change web.xml!
4. Write servlet and jsp code
servlet.java code
package com.njust.connect;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(name = "Servlet1",urlPatterns = {
"/Servlet1"})//解决404问题
public class Servlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String str=request.getParameter("character");
String name=new String(str.getBytes("ISO-8859-1"),"UTF-8");//解决中文乱码问题
System.out.println("doPost HELLO WORLD");
System.out.println(name);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("doGET HELLO WORLD");
}
}
The code of index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>标题</title>
</head>
<body>
最喜欢的
<form action="/Servlet1" method="post">//解决404问题
<input type="text" name="character" >
<input type="submit" >
</form>
好~
</body>
</html>
Specifically how to solve the problem of 404 and Chinese garbled characters will be explained in the sixth paragraph. If you want to read it directly, scroll down.
Next connect Tomcat, select Edit Configureations in the upper right corner
Green plus sign in the upper left corner -> TomcatServer -> Local
Give it a name, I changed it to MyTomcat. I don't like to check this After Launch, because I like to use UC browser (fast, UC makes money).
It's not over yet, Deployment->Artifact... add it!
It is useful if it looks like this.
5. start
Click the green triangle to load first. If tomcat is Chinese garbled, go to the root directory of tomcat to change xml, which has nothing to do with this article.
Enter localhost:8080 in the browser to connect to our index page
You can also directly enter http://localhost:8080/index.jsp
Enter the content, then click submit
to jump to a blank page,
The console outputs the above text, congratulations on your success
ps. Sonoda Umi is Yamato Nadeko in lovelive~
6.404 How to solve the Chinese garbled problem
First make sure your third largest segment
“Modules->Paths->Use module compile output path 选择我们在WEB-INF下面新建的classes文件夹。”
This step is correct. Otherwise, jsp may not find the servlet and report an error
then,
404 problem:
Please ensure that the urlPatterns of the servlet are equal to the action of the jsp! For example, my examples are all "/Servlet1" (I have been stuck on this for a long time, and finally learned it from my brother in the post bar)
@WebServlet(name = "Servlet1",urlPatterns = {
"/Servlet1"})//解决404问题
<form action="/Servlet1" method="post">
The problem of Chinese garbled characters when submitting the form (I output "Sonoda Haiwei", but the output is garbled characters): add these two sentences in the servlet
String str=request.getParameter("character");
String name=new String(str.getBytes("ISO-8859-1"),"UTF-8");
This is because the encoding used for its transmission is different from the encoding we commonly use. The function of these two sentences is conversion.
that's all