JSP jumps and transfers values to Servlet Complete project solves problems such as getparameter explosion, 404, Chinese garbled characters, etc.

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

insert image description here

insert image description here

To create a new project, choose ordinary java EE.
insert image description here
My project is called FirstConnect, and the built project should look like this.

insert image description here

2. Solve the problem of getparameter becoming popular

Find the path where you built the project in windows, and create two new folders.

insert image description here
Create a new folder \lib under the root directory
insert image description here

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
insert image description here

Copy to the lib directory we created under the project root directory

insert image description here

Next go back to intelij, click file->Project Structure

insert image description here

Next Modules->Dependencies->plus sign, 1.JARS or directories

insert image description here

Select the two jar packages in our project/lib and add them

insert image description here

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! insert image description hereModules->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.

insert image description here

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)

insert image description here

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)

insert image description here

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.
insert image description hereNext connect Tomcat, select Edit Configureations in the upper right corner

insert image description here
Green plus sign in the upper left corner -> TomcatServer -> Local

insert image description here
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).

insert image description here
It's not over yet, Deployment->Artifact... add it!

insert image description here
It is useful if it looks like this.

5. start

insert image description here
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.

insert image description here
Enter localhost:8080 in the browser to connect to our index page

You can also directly enter http://localhost:8080/index.jsp

insert image description here

Enter the content, then click submit
insert image description here
to jump to a blank page,
insert image description here

The console outputs the above text, congratulations on your success

ps. Sonoda Umi is Yamato Nadeko in lovelive~
insert image description here

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

Guess you like

Origin blog.csdn.net/qq_42915442/article/details/106461213